strings_spec.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. local t = require('test.unit.testutil')
  2. local itp = t.gen_itp(it)
  3. local cimport = t.cimport
  4. local eq = t.eq
  5. local ffi = t.ffi
  6. local to_cstr = t.to_cstr
  7. local strings = cimport('stdlib.h', './src/nvim/strings.h', './src/nvim/memory.h')
  8. describe('vim_strsave_escaped()', function()
  9. local vim_strsave_escaped = function(s, chars)
  10. local res = strings.vim_strsave_escaped(to_cstr(s), to_cstr(chars))
  11. local ret = ffi.string(res)
  12. -- Explicitly free memory so we are sure it is allocated: if it was not it
  13. -- will crash.
  14. strings.xfree(res)
  15. return ret
  16. end
  17. itp('precedes by a backslash all chars from second argument', function()
  18. eq([[\a\b\c\d]], vim_strsave_escaped('abcd', 'abcd'))
  19. end)
  20. itp('precedes by a backslash chars only from second argument', function()
  21. eq([[\a\bcd]], vim_strsave_escaped('abcd', 'ab'))
  22. end)
  23. itp('returns a copy of passed string if second argument is empty', function()
  24. eq('text \n text', vim_strsave_escaped('text \n text', ''))
  25. end)
  26. itp('returns an empty string if first argument is empty string', function()
  27. eq('', vim_strsave_escaped('', '\r'))
  28. end)
  29. itp('returns a copy of passed string if it does not contain chars from 2nd argument', function()
  30. eq('some text', vim_strsave_escaped('some text', 'a'))
  31. end)
  32. end)
  33. describe('vim_strnsave_unquoted()', function()
  34. local vim_strnsave_unquoted = function(s, len)
  35. local res = strings.vim_strnsave_unquoted(to_cstr(s), len or #s)
  36. local ret = ffi.string(res)
  37. -- Explicitly free memory so we are sure it is allocated: if it was not it
  38. -- will crash.
  39. strings.xfree(res)
  40. return ret
  41. end
  42. itp('copies unquoted strings as-is', function()
  43. eq('-c', vim_strnsave_unquoted('-c'))
  44. eq('', vim_strnsave_unquoted(''))
  45. end)
  46. itp('respects length argument', function()
  47. eq('', vim_strnsave_unquoted('-c', 0))
  48. eq('-', vim_strnsave_unquoted('-c', 1))
  49. eq('-', vim_strnsave_unquoted('"-c', 2))
  50. end)
  51. itp('unquotes fully quoted word', function()
  52. eq('/bin/sh', vim_strnsave_unquoted('"/bin/sh"'))
  53. end)
  54. itp('unquotes partially quoted word', function()
  55. eq('/Program Files/sh', vim_strnsave_unquoted('/Program" "Files/sh'))
  56. end)
  57. itp('removes ""', function()
  58. eq('/Program Files/sh', vim_strnsave_unquoted('/""Program" "Files/sh'))
  59. end)
  60. itp('performs unescaping of "', function()
  61. eq('/"Program Files"/sh', vim_strnsave_unquoted('/"\\""Program Files"\\""/sh'))
  62. end)
  63. itp('performs unescaping of \\', function()
  64. eq('/\\Program Files\\foo/sh', vim_strnsave_unquoted('/"\\\\"Program Files"\\\\foo"/sh'))
  65. end)
  66. itp('strips quote when there is no pair to it', function()
  67. eq('/Program Files/sh', vim_strnsave_unquoted('/Program" Files/sh'))
  68. eq('', vim_strnsave_unquoted('"'))
  69. end)
  70. itp('allows string to end with one backslash unescaped', function()
  71. eq('/Program Files/sh\\', vim_strnsave_unquoted('/Program" Files/sh\\'))
  72. end)
  73. itp('does not perform unescaping out of quotes', function()
  74. eq('/Program\\ Files/sh\\', vim_strnsave_unquoted('/Program\\ Files/sh\\'))
  75. end)
  76. itp('does not unescape \\n', function()
  77. eq('/Program\\nFiles/sh', vim_strnsave_unquoted('/Program"\\n"Files/sh'))
  78. end)
  79. end)
  80. describe('vim_strchr()', function()
  81. local vim_strchr = function(s, c)
  82. local str = to_cstr(s)
  83. local res = strings.vim_strchr(str, c)
  84. if res == nil then
  85. return nil
  86. else
  87. return res - str
  88. end
  89. end
  90. itp('handles NUL and <0 correctly', function()
  91. eq(nil, vim_strchr('abc', 0))
  92. eq(nil, vim_strchr('abc', -1))
  93. end)
  94. itp('works', function()
  95. eq(0, vim_strchr('abc', ('a'):byte()))
  96. eq(1, vim_strchr('abc', ('b'):byte()))
  97. eq(2, vim_strchr('abc', ('c'):byte()))
  98. eq(0, vim_strchr('a«b»c', ('a'):byte()))
  99. eq(3, vim_strchr('a«b»c', ('b'):byte()))
  100. eq(6, vim_strchr('a«b»c', ('c'):byte()))
  101. eq(nil, vim_strchr('«»', ('«'):byte()))
  102. -- 0xAB == 171 == '«'
  103. eq(nil, vim_strchr('\171', 0xAB))
  104. eq(0, vim_strchr('«»', 0xAB))
  105. eq(3, vim_strchr('„«»“', 0xAB))
  106. eq(7, vim_strchr('„«»“', 0x201C))
  107. eq(nil, vim_strchr('„«»“', 0x201D))
  108. eq(0, vim_strchr('„«»“', 0x201E))
  109. eq(0, vim_strchr('\244\143\188\128', 0x10FF00))
  110. eq(2, vim_strchr('«\244\143\188\128»', 0x10FF00))
  111. -- |0xDBFF |0xDF00 - surrogate pair for 0x10FF00
  112. eq(nil, vim_strchr('«\237\175\191\237\188\128»', 0x10FF00))
  113. end)
  114. end)
  115. describe('vim_snprintf()', function()
  116. local function a(expected, buf, bsize, fmt, ...)
  117. eq(#expected, strings.vim_snprintf(buf, bsize, fmt, ...))
  118. if bsize > 0 then
  119. local actual = ffi.string(buf, math.min(#expected + 1, bsize))
  120. eq(expected:sub(1, bsize - 1) .. '\0', actual)
  121. end
  122. end
  123. local function i(n)
  124. return ffi.cast('int', n)
  125. end
  126. local function l(n)
  127. return ffi.cast('long', n)
  128. end
  129. local function ll(n)
  130. return ffi.cast('long long', n)
  131. end
  132. local function z(n)
  133. return ffi.cast('ptrdiff_t', n)
  134. end
  135. local function u(n)
  136. return ffi.cast('unsigned', n)
  137. end
  138. local function ul(n)
  139. return ffi.cast('unsigned long', n)
  140. end
  141. local function ull(n)
  142. return ffi.cast('unsigned long long', n)
  143. end
  144. local function uz(n)
  145. return ffi.cast('size_t', n)
  146. end
  147. itp('truncation', function()
  148. for bsize = 0, 14 do
  149. local buf = ffi.gc(strings.xmalloc(bsize), strings.xfree)
  150. a('1.00000001e7', buf, bsize, '%.8g', 10000000.1)
  151. a('1234567', buf, bsize, '%d', i(1234567))
  152. a('1234567', buf, bsize, '%ld', l(1234567))
  153. a(' 1234567', buf, bsize, '%9ld', l(1234567))
  154. a('1234567 ', buf, bsize, '%-9ld', l(1234567))
  155. a('deadbeef', buf, bsize, '%x', u(0xdeadbeef))
  156. a('001100', buf, bsize, '%06b', u(12))
  157. a('one two', buf, bsize, '%s %s', 'one', 'two')
  158. a('1.234000', buf, bsize, '%f', 1.234)
  159. a('1.234000e+00', buf, bsize, '%e', 1.234)
  160. a('nan', buf, bsize, '%f', 0.0 / 0.0)
  161. a('inf', buf, bsize, '%f', 1.0 / 0.0)
  162. a('-inf', buf, bsize, '%f', -1.0 / 0.0)
  163. a('-0.000000', buf, bsize, '%f', tonumber('-0.0'))
  164. a('漢語', buf, bsize, '%s', '漢語')
  165. a(' 漢語', buf, bsize, '%8s', '漢語')
  166. a('漢語 ', buf, bsize, '%-8s', '漢語')
  167. a('漢', buf, bsize, '%.3s', '漢語')
  168. a(' foo', buf, bsize, '%5S', 'foo')
  169. a('%%%', buf, bsize, '%%%%%%')
  170. a('0x87654321', buf, bsize, '%p', ffi.cast('char *', 0x87654321))
  171. a('0x0087654321', buf, bsize, '%012p', ffi.cast('char *', 0x87654321))
  172. end
  173. end)
  174. itp('positional arguments', function()
  175. for bsize = 0, 24 do
  176. local buf = ffi.gc(strings.xmalloc(bsize), strings.xfree)
  177. a('1234567 ', buf, bsize, '%1$*2$ld', l(1234567), i(-9))
  178. a('1234567 ', buf, bsize, '%1$*2$.*3$ld', l(1234567), i(-9), i(5))
  179. a('1234567 ', buf, bsize, '%1$*3$.*2$ld', l(1234567), i(5), i(-9))
  180. a('1234567 ', buf, bsize, '%3$*1$.*2$ld', i(-9), i(5), l(1234567))
  181. a('1234567', buf, bsize, '%1$ld', l(1234567))
  182. a(' 1234567', buf, bsize, '%1$*2$ld', l(1234567), i(9))
  183. a('9 12345 7654321', buf, bsize, '%2$ld %1$d %3$lu', i(12345), l(9), ul(7654321))
  184. a('9 1234567 7654321', buf, bsize, '%2$d %1$ld %3$lu', l(1234567), i(9), ul(7654321))
  185. a('9 1234567 7654321', buf, bsize, '%2$d %1$lld %3$lu', ll(1234567), i(9), ul(7654321))
  186. a('9 12345 7654321', buf, bsize, '%2$ld %1$u %3$lu', u(12345), l(9), ul(7654321))
  187. a('9 1234567 7654321', buf, bsize, '%2$d %1$lu %3$lu', ul(1234567), i(9), ul(7654321))
  188. a('9 1234567 7654321', buf, bsize, '%2$d %1$llu %3$lu', ull(1234567), i(9), ul(7654321))
  189. a('9 deadbeef 7654321', buf, bsize, '%2$d %1$x %3$lu', u(0xdeadbeef), i(9), ul(7654321))
  190. a('9 c 7654321', buf, bsize, '%2$ld %1$c %3$lu', i(('c'):byte()), l(9), ul(7654321))
  191. a('9 hi 7654321', buf, bsize, '%2$ld %1$s %3$lu', 'hi', l(9), ul(7654321))
  192. a('9 0.000000e+00 7654321', buf, bsize, '%2$ld %1$e %3$lu', 0.0, l(9), ul(7654321))
  193. a('two one two', buf, bsize, '%2$s %1$s %2$s', 'one', 'two', 'three')
  194. a('three one two', buf, bsize, '%3$s %1$s %2$s', 'one', 'two', 'three')
  195. a('1234567', buf, bsize, '%1$d', i(1234567))
  196. a('deadbeef', buf, bsize, '%1$x', u(0xdeadbeef))
  197. a('001100', buf, bsize, '%2$0*1$b', i(6), u(12))
  198. a('001100', buf, bsize, '%1$0.*2$b', u(12), i(6))
  199. a('one two', buf, bsize, '%1$s %2$s', 'one', 'two')
  200. a('001100', buf, bsize, '%06b', u(12))
  201. a('two one', buf, bsize, '%2$s %1$s', 'one', 'two')
  202. a('1.234000', buf, bsize, '%1$f', 1.234)
  203. a('1.234000e+00', buf, bsize, '%1$e', 1.234)
  204. a('nan', buf, bsize, '%1$f', 0.0 / 0.0)
  205. a('inf', buf, bsize, '%1$f', 1.0 / 0.0)
  206. a('-inf', buf, bsize, '%1$f', -1.0 / 0.0)
  207. a('-0.000000', buf, bsize, '%1$f', tonumber('-0.0'))
  208. end
  209. end)
  210. itp('%zd and %zu', function()
  211. local bsize = 20
  212. local buf = ffi.gc(strings.xmalloc(bsize), strings.xfree)
  213. a('-1234567 -7654321', buf, bsize, '%zd %zd', z(-1234567), z(-7654321))
  214. a('-7654321 -1234567', buf, bsize, '%2$zd %1$zd', z(-1234567), z(-7654321))
  215. a('1234567 7654321', buf, bsize, '%zu %zu', uz(1234567), uz(7654321))
  216. a('7654321 1234567', buf, bsize, '%2$zu %1$zu', uz(1234567), uz(7654321))
  217. end)
  218. end)
  219. describe('strcase_save()', function()
  220. local strcase_save = function(input_string, upper)
  221. local res = strings.strcase_save(to_cstr(input_string), upper)
  222. return ffi.string(res)
  223. end
  224. itp('decodes overlong encoded characters.', function()
  225. eq('A', strcase_save('\xc1\x81', true))
  226. eq('a', strcase_save('\xc1\x81', false))
  227. end)
  228. end)
  229. describe('reverse_text', function()
  230. local reverse_text = function(str)
  231. return t.internalize(strings.reverse_text(to_cstr(str)))
  232. end
  233. itp('handles empty string', function()
  234. eq('', reverse_text(''))
  235. end)
  236. itp('handles simple cases', function()
  237. eq('a', reverse_text('a'))
  238. eq('ba', reverse_text('ab'))
  239. end)
  240. itp('handles multibyte characters', function()
  241. eq('bα', reverse_text('αb'))
  242. eq('Yötön yö', reverse_text('öy nötöY'))
  243. end)
  244. itp('handles combining chars', function()
  245. local utf8_COMBINING_RING_ABOVE = '\204\138'
  246. local utf8_COMBINING_RING_BELOW = '\204\165'
  247. eq(
  248. 'bba' .. utf8_COMBINING_RING_ABOVE .. utf8_COMBINING_RING_BELOW .. 'aa',
  249. reverse_text('aaa' .. utf8_COMBINING_RING_ABOVE .. utf8_COMBINING_RING_BELOW .. 'bb')
  250. )
  251. end)
  252. itp('treats invalid utf as separate characters', function()
  253. eq('\192ba', reverse_text('ab\192'))
  254. end)
  255. itp('treats an incomplete utf continuation sequence as valid', function()
  256. eq('\194ba', reverse_text('ab\194'))
  257. end)
  258. end)