stdstringlib.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. .. _stdlib_stdstringlib:
  2. ==================
  3. The String library
  4. ==================
  5. the string lib implements string formatting and regular expression matching routines.
  6. --------------
  7. Squirrel API
  8. --------------
  9. ++++++++++++++
  10. Global Symbols
  11. ++++++++++++++
  12. .. js:function:: endswith(str, cmp)
  13. returns `true` if the end of the string `str` matches a the string `cmp` otherwise returns `false`
  14. .. js:function:: ecape(str)
  15. Returns a string with backslashes before characters that need to be escaped(`\",\a,\b,\t,\n,\v,\f,\r,\\,\",\',\0,\xnn`).
  16. .. js:function:: format(formatstr, ...)
  17. Returns a string formatted according `formatstr` and the optional parameters following it.
  18. The format string follows the same rules as the `printf` family of
  19. standard C functions( the "*" is not supported). ::
  20. eg.
  21. sq> print(format("%s %d 0x%02X\n","this is a test :",123,10));
  22. this is a test : 123 0x0A
  23. .. js:function:: lstrip(str)
  24. Strips white-space-only characters that might appear at the beginning of the given string
  25. and returns the new stripped string.
  26. .. js:function:: rstrip(str)
  27. Strips white-space-only characters that might appear at the end of the given string
  28. and returns the new stripped string.
  29. .. js:function:: split(str, separators)
  30. returns an array of strings split at each point where a separator character occurs in `str`.
  31. The separator is not returned as part of any array element.
  32. The parameter `separators` is a string that specifies the characters as to be used for the splitting.
  33. ::
  34. eg.
  35. local a = split("1.2-3;4/5",".-/;");
  36. // the result will be [1,2,3,4,5]
  37. .. js:function:: startswith(str, cmp)
  38. returns `true` if the beginning of the string `str` matches the string `cmp`; otherwise returns `false`
  39. .. js:function:: strip(str)
  40. Strips white-space-only characters that might appear at the beginning or end of the given string and returns the new stripped string.
  41. ++++++++++++++++++
  42. The regexp class
  43. ++++++++++++++++++
  44. .. js:class:: regexp(pattern)
  45. The regexp object represent a precompiled regular expression pattern. The object is created
  46. trough `regexp(patern)`.
  47. +---------------------+--------------------------------------+
  48. | `\\` | Quote the next metacharacter |
  49. +---------------------+--------------------------------------+
  50. | `^` | Match the beginning of the string |
  51. +---------------------+--------------------------------------+
  52. | `.` | Match any character |
  53. +---------------------+--------------------------------------+
  54. | `$` | Match the end of the string |
  55. +---------------------+--------------------------------------+
  56. | `|` | Alternation |
  57. +---------------------+--------------------------------------+
  58. | `(subexp)` | Grouping (creates a capture) |
  59. +---------------------+--------------------------------------+
  60. | `(?:subexp)` | No Capture Grouping (no capture) |
  61. +---------------------+--------------------------------------+
  62. | `[]` | Character class |
  63. +---------------------+--------------------------------------+
  64. **GREEDY CLOSURES**
  65. +---------------------+---------------------------------------------+
  66. | `*` | Match 0 or more times |
  67. +---------------------+---------------------------------------------+
  68. | `+` | Match 1 or more times |
  69. +---------------------+---------------------------------------------+
  70. | `?` | Match 1 or 0 times |
  71. +---------------------+---------------------------------------------+
  72. | `{n}` | Match exactly n times |
  73. +---------------------+---------------------------------------------+
  74. | `{n,}` | Match at least n times |
  75. +---------------------+---------------------------------------------+
  76. | `{n,m}` | Match at least n but not more than m times |
  77. +---------------------+---------------------------------------------+
  78. **ESCAPE CHARACTERS**
  79. +---------------------+--------------------------------------+
  80. | `\\t` | tab (HT, TAB) |
  81. +---------------------+--------------------------------------+
  82. | `\\n` | newline (LF, NL) |
  83. +---------------------+--------------------------------------+
  84. | `\\r` | return (CR) |
  85. +---------------------+--------------------------------------+
  86. | `\\f` | form feed (FF) |
  87. +---------------------+--------------------------------------+
  88. **PREDEFINED CLASSES**
  89. +---------------------+--------------------------------------+
  90. | `\\l` | lowercase next char |
  91. +---------------------+--------------------------------------+
  92. | `\\u` | uppercase next char |
  93. +---------------------+--------------------------------------+
  94. | `\\a` | letters |
  95. +---------------------+--------------------------------------+
  96. | `\\A` | non letters |
  97. +---------------------+--------------------------------------+
  98. | `\\w` | alphanumeric `[_0-9a-zA-Z]` |
  99. +---------------------+--------------------------------------+
  100. | `\\W` | non alphanumeric `[^_0-9a-zA-Z]` |
  101. +---------------------+--------------------------------------+
  102. | `\\s` | space |
  103. +---------------------+--------------------------------------+
  104. | `\\S` | non space |
  105. +---------------------+--------------------------------------+
  106. | `\\d` | digits |
  107. +---------------------+--------------------------------------+
  108. | `\\D` | non digits |
  109. +---------------------+--------------------------------------+
  110. | `\\x` | hexadecimal digits |
  111. +---------------------+--------------------------------------+
  112. | `\\X` | non hexadecimal digits |
  113. +---------------------+--------------------------------------+
  114. | `\\c` | control characters |
  115. +---------------------+--------------------------------------+
  116. | `\\C` | non control characters |
  117. +---------------------+--------------------------------------+
  118. | `\\p` | punctuation |
  119. +---------------------+--------------------------------------+
  120. | `\\P` | non punctuation |
  121. +---------------------+--------------------------------------+
  122. | `\\b` | word boundary |
  123. +---------------------+--------------------------------------+
  124. | `\\B` | non word boundary |
  125. +---------------------+--------------------------------------+
  126. .. js:function:: regexp.capture(str [, start])
  127. returns an array of tables containing two indexes ("begin" and "end") of
  128. the first match of the regular expression in the string `str`.
  129. An array entry is created for each captured sub expressions. If no match occurs returns null.
  130. The search starts from the index `start`
  131. of the string; if `start` is omitted the search starts from the beginning of the string.
  132. The first element of the returned array(index 0) always contains the complete match.
  133. ::
  134. local ex = regexp(@"(\d+) ([a-zA-Z]+)(\p)");
  135. local string = "stuff 123 Test;";
  136. local res = ex.capture(string);
  137. foreach(i,val in res)
  138. {
  139. print(format("match number[%02d] %s\n",
  140. i,string.slice(val.begin,val.end))); //prints "Test"
  141. }
  142. ...
  143. will print
  144. match number[00] 123 Test;
  145. match number[01] 123
  146. match number[02] Test
  147. match number[03] ;
  148. .. js:function:: regexp.match(str)
  149. returns a true if the regular expression matches the string
  150. `str`, otherwise returns false.
  151. .. js:function:: regexp.search(str [, start])
  152. returns a table containing two indexes ("begin" and "end") of the first match of the regular expression in
  153. the string `str`, otherwise if no match occurs returns null. The search starts from the index `start`
  154. of the string; if `start` is omitted the search starts from the beginning of the string.
  155. ::
  156. local ex = regexp("[a-zA-Z]+");
  157. local string = "123 Test;";
  158. local res = ex.search(string);
  159. print(string.slice(res.begin,res.end)); //prints "Test"
  160. -------------
  161. C API
  162. -------------
  163. .. _sqstd_register_stringlib:
  164. .. c:function:: SQRESULT sqstd_register_stringlib(HSQUIRRELVM v)
  165. :param HSQUIRRELVM v: the target VM
  166. :returns: an SQRESULT
  167. :remarks: The function aspects a table on top of the stack where to register the global library functions.
  168. initialize and register the string library in the given VM.
  169. +++++++++++++
  170. Formatting
  171. +++++++++++++
  172. .. c:function:: SQRESULT sqstd_format(HSQUIRRELVM v, SQInteger nformatstringidx, SQInteger* outlen, SQChar** output)
  173. :param HSQUIRRELVM v: the target VM
  174. :param SQInteger nformatstringidx: index in the stack of the format string
  175. :param SQInteger* outlen: a pointer to an integer that will be filled with the length of the newly created string
  176. :param SQChar** output: a pointer to a string pointer that will receive the newly created string
  177. :returns: an SQRESULT
  178. :remarks: the newly created string is allocated in the scratchpad memory.
  179. creates a new string formatted according to the object at position `nformatstringidx` and the optional parameters following it.
  180. The format string follows the same rules as the `printf` family of
  181. standard C functions( the "*" is not supported).
  182. ++++++++++++++++++
  183. Regular Expessions
  184. ++++++++++++++++++
  185. .. c:function:: SQRex* sqstd_rex_compile(const SQChar *pattern, const SQChar ** error)
  186. :param SQChar* pattern: a pointer to a zero terminated string containing the pattern that has to be compiled.
  187. :param SQChar** error: a pointer to a string pointer that will be set with an error string in case of failure.
  188. :returns: a pointer to the compiled pattern
  189. compiles an expression and returns a pointer to the compiled version.
  190. in case of failure returns NULL.The returned object has to be deleted
  191. through the function sqstd_rex_free().
  192. .. c:function:: void sqstd_rex_free(SQRex * exp)
  193. :param SQRex* exp: the expression structure that has to be deleted.
  194. deletes a expression structure created with sqstd_rex_compile()
  195. .. c:function:: SQBool sqstd_rex_match(SQRex * exp,const SQChar * text)
  196. :param SQRex* exp: a compiled expression
  197. :param SQChar* text: the string that has to be tested
  198. :returns: SQTrue if successful otherwise SQFalse
  199. returns SQTrue if the string specified in the parameter text is an
  200. exact match of the expression, otherwise returns SQFalse.
  201. .. c:function:: SQBool sqstd_rex_search(SQRex * exp, const SQChar * text, const SQChar ** out_begin, const SQChar ** out_end)
  202. :param SQRex* exp: a compiled expression
  203. :param SQChar* text: the string that has to be tested
  204. :param SQChar** out_begin: a pointer to a string pointer that will be set with the beginning of the match
  205. :param SQChar** out_end: a pointer to a string pointer that will be set with the end of the match
  206. :returns: SQTrue if successful otherwise SQFalse
  207. searches the first match of the expression in the string specified in the parameter text.
  208. if the match is found returns SQTrue and the sets out_begin to the beginning of the
  209. match and out_end at the end of the match; otherwise returns SQFalse.
  210. .. c:function:: SQBool sqstd_rex_searchrange(SQRex * exp, const SQChar * text_begin, const SQChar * text_end, const SQChar ** out_begin, const SQChar ** out_end)
  211. :param SQRex* exp: a compiled expression
  212. :param SQChar* text_begin: a pointer to the beginnning of the string that has to be tested
  213. :param SQChar* text_end: a pointer to the end of the string that has to be tested
  214. :param SQChar** out_begin: a pointer to a string pointer that will be set with the beginning of the match
  215. :param SQChar** out_end: a pointer to a string pointer that will be set with the end of the match
  216. :returns: SQTrue if successful otherwise SQFalse
  217. searches the first match of the expression in the string delimited
  218. by the parameter text_begin and text_end.
  219. if the match is found returns SQTrue and sets out_begin to the beginning of the
  220. match and out_end at the end of the match; otherwise returns SQFalse.
  221. .. c:function:: SQInteger sqstd_rex_getsubexpcount(SQRex * exp)
  222. :param SQRex* exp: a compiled expression
  223. :returns: the number of sub expressions matched by the expression
  224. returns the number of sub expressions matched by the expression
  225. .. c:function:: SQBool sqstd_rex_getsubexp(SQRex * exp, SQInteger n, SQRexMatch *subexp)
  226. :param SQRex* exp: a compiled expression
  227. :param SQInteger n: the index of the submatch(0 is the complete match)
  228. :param SQRexMatch* a: pointer to structure that will store the result
  229. :returns: the function returns SQTrue if n is a valid index; otherwise SQFalse.
  230. retrieve the begin and and pointer to the length of the sub expression indexed
  231. by n. The result is passed through the struct SQRexMatch.