strings_spec.lua 11 KB

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