stdstringlib.rst 13 KB

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