tgcdestructors.nim 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. discard """
  2. cmd: '''nim c -d:nimAllocStats --newruntime $file'''
  3. output: '''hi
  4. ho
  5. ha
  6. @["arg", "asdfklasdfkl", "asdkfj", "dfasj", "klfjl"]
  7. @[1, 2, 3]
  8. @["red", "yellow", "orange", "rtrt1", "pink"]
  9. a: @[4, 2, 3]
  10. 0
  11. 30
  12. true
  13. (allocCount: 27, deallocCount: 27)'''
  14. """
  15. include system / ansi_c
  16. proc main =
  17. var s: seq[string] = @[]
  18. for i in 0..<80: s.add "foo"
  19. main()
  20. const
  21. test = @["hi", "ho", "ha"]
  22. for t in test:
  23. echo t
  24. type
  25. InterpolatedKind* = enum
  26. ikStr, ## ``str`` part of the interpolated string
  27. ikDollar, ## escaped ``$`` part of the interpolated string
  28. ikVar, ## ``var`` part of the interpolated string
  29. ikExpr ## ``expr`` part of the interpolated string
  30. iterator interpolatedFragments*(s: string): tuple[kind: InterpolatedKind,
  31. value: string] =
  32. var i = 0
  33. var kind: InterpolatedKind
  34. while true:
  35. var j = i
  36. if j < s.len and s[j] == '$':
  37. if j+1 < s.len and s[j+1] == '{':
  38. inc j, 2
  39. var nesting = 0
  40. block curlies:
  41. while j < s.len:
  42. case s[j]
  43. of '{': inc nesting
  44. of '}':
  45. if nesting == 0:
  46. inc j
  47. break curlies
  48. dec nesting
  49. else: discard
  50. inc j
  51. raise newException(ValueError,
  52. "Expected closing '}': " & substr(s, i, s.high))
  53. inc i, 2 # skip ${
  54. kind = ikExpr
  55. elif j+1 < s.len and s[j+1] in {'A'..'Z', 'a'..'z', '_'}:
  56. inc j, 2
  57. while j < s.len and s[j] in {'A'..'Z', 'a'..'z', '0'..'9', '_'}: inc(j)
  58. inc i # skip $
  59. kind = ikVar
  60. elif j+1 < s.len and s[j+1] == '$':
  61. inc j, 2
  62. inc i # skip $
  63. kind = ikDollar
  64. else:
  65. raise newException(ValueError,
  66. "Unable to parse a varible name at " & substr(s, i, s.high))
  67. else:
  68. while j < s.len and s[j] != '$': inc j
  69. kind = ikStr
  70. if j > i:
  71. # do not copy the trailing } for ikExpr:
  72. yield (kind, substr(s, i, j-1-ord(kind == ikExpr)))
  73. else:
  74. break
  75. i = j
  76. proc parseCmdLine(c: string): seq[string] =
  77. result = @[]
  78. var i = 0
  79. var a = ""
  80. while true:
  81. setLen(a, 0)
  82. while i < c.len and c[i] in {' ', '\t', '\l', '\r'}: inc(i)
  83. if i >= c.len: break
  84. var inQuote = false
  85. while i < c.len:
  86. case c[i]
  87. of '\\':
  88. var j = i
  89. while j < c.len and c[j] == '\\': inc(j)
  90. if j < c.len and c[j] == '"':
  91. for k in 1..(j-i) div 2: a.add('\\')
  92. if (j-i) mod 2 == 0:
  93. i = j
  94. else:
  95. a.add('"')
  96. i = j+1
  97. else:
  98. a.add(c[i])
  99. inc(i)
  100. of '"':
  101. inc(i)
  102. if not inQuote: inQuote = true
  103. elif i < c.len and c[i] == '"':
  104. a.add(c[i])
  105. inc(i)
  106. else:
  107. inQuote = false
  108. break
  109. of ' ', '\t':
  110. if not inQuote: break
  111. a.add(c[i])
  112. inc(i)
  113. else:
  114. a.add(c[i])
  115. inc(i)
  116. add(result, a)
  117. proc other =
  118. let input = "$test{} $this is ${an{ example}} "
  119. let expected = @[(ikVar, "test"), (ikStr, "{} "), (ikVar, "this"),
  120. (ikStr, " is "), (ikExpr, "an{ example}"), (ikStr, " ")]
  121. var i = 0
  122. for s in interpolatedFragments(input):
  123. doAssert s == expected[i]
  124. inc i
  125. echo parseCmdLine("arg asdfklasdfkl asdkfj dfasj klfjl")
  126. other()
  127. # bug #11050
  128. type
  129. Obj* = object
  130. f*: seq[int]
  131. method main(o: Obj) {.base.} =
  132. for newb in o.f:
  133. discard
  134. # test that o.f was not moved!
  135. proc testforNoMove =
  136. var o = Obj(f: @[1, 2, 3])
  137. main(o)
  138. echo o.f
  139. testforNoMove()
  140. # bug #11065
  141. type
  142. Warm = seq[string]
  143. proc testWarm =
  144. var w: Warm
  145. w = @["red", "yellow", "orange"]
  146. var x = "rt"
  147. var y = "rt1"
  148. w.add(x & y)
  149. w.add("pink")
  150. echo w
  151. testWarm()
  152. proc mutConstSeq() =
  153. # bug #11524
  154. var a = @[1,2,3]
  155. a[0] = 4
  156. echo "a: ", a
  157. mutConstSeq()
  158. proc mainSeqOfCap =
  159. # bug #11098
  160. var s = newSeqOfCap[int](10)
  161. echo s.len
  162. var s2 = newSeqUninitialized[int](30)
  163. echo s2.len
  164. mainSeqOfCap()
  165. # bug #11614
  166. let ga = "foo"
  167. proc takeAinArray =
  168. let b = [ga]
  169. takeAinArray()
  170. echo ga == "foo"
  171. echo getAllocStats()