tamemfiles.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. discard """
  2. output: '''
  3. loop 1a
  4. loop 1b; cols: @[1, x]
  5. loop 1c
  6. loop 1d
  7. loop 1a
  8. loop 1b; cols: @[2, y]
  9. loop 1c
  10. loop 1d
  11. '''
  12. cmd: "nim c --gc:arc $file"
  13. """
  14. # bug #13596
  15. import tables, memfiles, strutils, os
  16. type Splitr* = tuple[ repeat: bool, chrDlm: char, setDlm: set[char], n: int ]
  17. type csize = uint
  18. proc cmemchr*(s: pointer, c: char, n: csize): pointer {.
  19. importc: "memchr", header: "<string.h>" .}
  20. proc `-!`*(p, q: pointer): int {.inline.} =
  21. (cast[int](p) -% cast[int](q)).int
  22. proc `+!`*(p: pointer, i: int): pointer {.inline.} =
  23. cast[pointer](cast[int](p) +% i)
  24. proc `+!`*(p: pointer, i: uint64): pointer {.inline.} =
  25. cast[pointer](cast[uint64](p) + i)
  26. proc charEq(x, c: char): bool {.inline.} = x == c
  27. proc initSplitr*(delim: string): Splitr =
  28. if delim == "white": #User can use any other permutation if needed
  29. result.repeat = true
  30. result.chrDlm = ' '
  31. result.setDlm = { ' ', '\t', '\n' }
  32. result.n = result.setDlm.card
  33. return
  34. for c in delim:
  35. if c in result.setDlm:
  36. result.repeat = true
  37. continue
  38. result.setDlm.incl(c)
  39. inc(result.n)
  40. if result.n == 1: #support n==1 test to allow memchr optimization
  41. result.chrDlm = delim[0]
  42. proc hash(x: MemSlice): int = 55542
  43. template defSplit[T](slc: T, fs: var seq[MemSlice], n: int, repeat: bool,
  44. sep: untyped, nextSep: untyped, isSep: untyped) {.dirty.} =
  45. fs.setLen(if n < 1: 16 else: n)
  46. var b = slc.data
  47. var eob = b +! slc.size
  48. while repeat and eob -! b > 0 and isSep((cast[cstring](b))[0], sep):
  49. b = b +! 1
  50. if b == eob: fs.setLen(0); return
  51. var e = nextSep(b, sep, (eob -! b).csize)
  52. while e != nil:
  53. if n < 1: #Unbounded msplit
  54. if result == fs.len - 1: #Expand capacity
  55. fs.setLen(if fs.len < 512: 2*fs.len else: fs.len + 512)
  56. elif result == n - 1: #Need 1 more slot for final field
  57. break
  58. fs[result].data = b
  59. fs[result].size = e -! b
  60. result += 1
  61. while repeat and eob -! e > 0 and isSep((cast[cstring](e))[1], sep):
  62. e = e +! 1
  63. b = e +! 1
  64. if eob -! b <= 0:
  65. b = eob
  66. break
  67. e = nextSep(b, sep, (eob -! b).csize)
  68. if not repeat or eob -! b > 0:
  69. fs[result].data = b
  70. fs[result].size = eob -! b
  71. result += 1
  72. fs.setLen(result)
  73. proc msplit*(s: MemSlice, fs: var seq[MemSlice], sep=' ', n=0,
  74. repeat=false): int =
  75. defSplit(s, fs, n, repeat, sep, cmemchr, charEq)
  76. proc split*(s: Splitr, line: MemSlice, cols: var seq[MemSlice],
  77. n=0) {.inline.} =
  78. discard msplit(line, cols, s.chrDlm, n, s.repeat)
  79. ########################################################################
  80. # Using lines instead of memSlices & split instead of splitr.split seems
  81. # to mask the arc problem, as does simplifying `Table` to `seq[char]`.
  82. proc load(path: string, delim=" "): Table[MemSlice, seq[char]] =
  83. let f = memfiles.open(path)
  84. let splitr = initSplitr(delim)
  85. var cols: seq[MemSlice] = @[ ] # re-used seq buffer
  86. var nwSq = newSeqOfCap[char](1) # re-used seq value
  87. nwSq.setLen 1
  88. for line in memSlices(f, eat='\0'):
  89. stderr.write "loop 1a\n"
  90. splitr.split(line, cols, 2)
  91. stderr.write "loop 1b; cols: ", cols, "\n"
  92. let cs = cast[cstring](cols[0].data)
  93. stderr.write "loop 1c\n" #..reports exception here, but
  94. nwSq[0] = cs[0] #..actually doing out of bounds here
  95. stderr.write "loop 1d\n"
  96. result[cols[1]] = nwSq
  97. discard load(getAppDir() / "testfile.txt")