tptrarrayderef.nim 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. discard """
  2. output: '''[1, 2, 3, 4]
  3. 3
  4. ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C']
  5. OK
  6. '''
  7. """
  8. var
  9. arr = [1,2,3]
  10. arrp = addr(arr)
  11. sss = @[4,5,6,7]
  12. sssp = addr(sss)
  13. ra = new(array[3, int])
  14. raa = [11,12,13]
  15. #bug #3586
  16. proc mutate[T](arr:openArray[T], brr: openArray[T]) =
  17. for i in 0..arr.len-1:
  18. doAssert(arr[i] == brr[i])
  19. mutate(arr, arr)
  20. #bug #2240
  21. proc f(a: openArray[int], b: openArray[int]) =
  22. for i in 0..a.len-1:
  23. doAssert(a[i] == b[i])
  24. var a = [7,8,9]
  25. var p = addr a
  26. f(p[], a)
  27. f(sssp[], sss)
  28. ra[0] = 11
  29. ra[1] = 12
  30. ra[2] = 13
  31. f(ra[], raa)
  32. #bug #2240b
  33. proc fillBuffer(buf: var openArray[char]) =
  34. for i in 0..buf.len-1:
  35. buf[i] = chr(i)
  36. proc fillSeqBuffer(b: ref seq[char]) =
  37. fillBuffer(b[])
  38. proc getFilledBuffer(sz: int): ref seq[char] =
  39. let s : ref seq[char] = new(seq[char])
  40. s[] = newSeq[char](sz)
  41. fillBuffer(s[])
  42. return s
  43. let aa = getFilledBuffer(3)
  44. for i in 0..aa[].len-1:
  45. doAssert(aa[i] == chr(i))
  46. var
  47. x = [1, 2, 3, 4]
  48. y1 = block: (
  49. a: (block:
  50. echo x
  51. cast[ptr array[2, int]](addr(x[0]))[]),
  52. b: 3)
  53. y2 = block:
  54. echo y1.a[0] + y1.a[1]
  55. cast[ptr array[4, int]](addr(x))[]
  56. doAssert y1 == ([1, 2], 3)
  57. doAssert y2 == [1, 2, 3, 4]
  58. template newOpenArray(x: var string, size: int): openArray[char] =
  59. var z = 1
  60. toOpenArray(x, z, size)
  61. template doSomethingAndCreate(x: var string): openArray[char] =
  62. let size = 12
  63. newOpenArray(x, size)
  64. proc sinkk(x: openArray[char]) =
  65. echo x
  66. var xArrayDeref = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  67. sinkk doSomethingAndCreate(xArrayDeref)
  68. echo "OK"