PageRenderTime 12ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/jsstr.h

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C++ Header | 662 lines | 318 code | 99 blank | 245 comment | 42 complexity | 81d3efe739ae057c74a8e7d55413056b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  3. * ***** BEGIN LICENSE BLOCK *****
  4. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is Mozilla Communicator client code, released
  17. * March 31, 1998.
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Netscape Communications Corporation.
  21. * Portions created by the Initial Developer are Copyright (C) 1998
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. *
  26. * Alternatively, the contents of this file may be used under the terms of
  27. * either of the GNU General Public License Version 2 or later (the "GPL"),
  28. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29. * in which case the provisions of the GPL or the LGPL are applicable instead
  30. * of those above. If you wish to allow use of your version of this file only
  31. * under the terms of either the GPL or the LGPL, and not to allow others to
  32. * use your version of this file under the terms of the MPL, indicate your
  33. * decision by deleting the provisions above and replace them with the notice
  34. * and other provisions required by the GPL or the LGPL. If you do not delete
  35. * the provisions above, a recipient may use your version of this file under
  36. * the terms of any one of the MPL, the GPL or the LGPL.
  37. *
  38. * ***** END LICENSE BLOCK ***** */
  39. #ifndef jsstr_h___
  40. #define jsstr_h___
  41. /*
  42. * JS string type implementation.
  43. *
  44. * A JS string is a counted array of unicode characters. To support handoff
  45. * of API client memory, the chars are allocated separately from the length,
  46. * necessitating a pointer after the count, to form a separately allocated
  47. * string descriptor. String descriptors are GC'ed, while their chars are
  48. * allocated from the malloc heap.
  49. */
  50. #include <ctype.h>
  51. #include "jspubtd.h"
  52. #include "jsprvtd.h"
  53. JS_BEGIN_EXTERN_C
  54. /*
  55. * The GC-thing "string" type.
  56. *
  57. * When the JSSTRFLAG_DEPENDENT bit of the length field is unset, the u.chars
  58. * field points to a flat character array owned by its GC-thing descriptor.
  59. * The array is terminated at index length by a zero character and the size of
  60. * the array in bytes is (length + 1) * sizeof(jschar). The terminator is
  61. * purely a backstop, in case the chars pointer flows out to native code that
  62. * requires \u0000 termination.
  63. *
  64. * A flat string with JSSTRFLAG_MUTABLE set means that the string is accessible
  65. * only from one thread and it is possible to turn it into a dependent string
  66. * of the same length to optimize js_ConcatStrings. It is also possible to grow
  67. * such a string, but extreme care must be taken to ensure that no other code
  68. * relies on the original length of the string.
  69. *
  70. * A flat string with JSSTRFLAG_ATOMIZED set means that the string is hashed as
  71. * an atom. This flag is used to avoid re-hashing the already-atomized string.
  72. *
  73. * When JSSTRFLAG_DEPENDENT is set, the string depends on characters of another
  74. * string strongly referenced by the u.base field. The base member may point to
  75. * another dependent string if JSSTRING_CHARS has not been called yet.
  76. *
  77. * JSSTRFLAG_PREFIX determines the kind of the dependent string. When the flag
  78. * is unset, the length field encodes both starting position relative to the
  79. * base string and the number of characters in the dependent string, see
  80. * JSSTRDEP_START_MASK and JSSTRDEP_LENGTH_MASK macros below for details.
  81. *
  82. * When JSSTRFLAG_PREFIX is set, the dependent string is a prefix of the base
  83. * string. The number of characters in the prefix is encoded using all non-flag
  84. * bits of the length field and spans the same 0 .. SIZE_T_MAX/4 range as the
  85. * length of the flat string.
  86. *
  87. * NB: Always use the JSSTRING_LENGTH and JSSTRING_CHARS accessor macros.
  88. */
  89. struct JSString {
  90. size_t length;
  91. union {
  92. jschar *chars;
  93. JSString *base;
  94. } u;
  95. };
  96. /*
  97. * Definitions for flags stored in the high order bits of JSString.length.
  98. * JSSTRFLAG_PREFIX and JSSTRFLAG_MUTABLE are two aliases for the same value.
  99. * JSSTRFLAG_PREFIX should be used only if JSSTRFLAG_DEPENDENT is set and
  100. * JSSTRFLAG_MUTABLE should be used only if the string is flat.
  101. * JSSTRFLAG_ATOMIZED is used only with the flat immutable strings.
  102. */
  103. #define JSSTRFLAG_DEPENDENT JSSTRING_BIT(JS_BITS_PER_WORD - 1)
  104. #define JSSTRFLAG_PREFIX JSSTRING_BIT(JS_BITS_PER_WORD - 2)
  105. #define JSSTRFLAG_MUTABLE JSSTRFLAG_PREFIX
  106. #define JSSTRFLAG_ATOMIZED JSSTRING_BIT(JS_BITS_PER_WORD - 3)
  107. #define JSSTRING_LENGTH_BITS (JS_BITS_PER_WORD - 3)
  108. #define JSSTRING_LENGTH_MASK JSSTRING_BITMASK(JSSTRING_LENGTH_BITS)
  109. /* Universal JSString type inquiry and accessor macros. */
  110. #define JSSTRING_BIT(n) ((size_t)1 << (n))
  111. #define JSSTRING_BITMASK(n) (JSSTRING_BIT(n) - 1)
  112. #define JSSTRING_HAS_FLAG(str,flg) ((str)->length & (flg))
  113. #define JSSTRING_IS_DEPENDENT(str) JSSTRING_HAS_FLAG(str, JSSTRFLAG_DEPENDENT)
  114. #define JSSTRING_IS_FLAT(str) (!JSSTRING_IS_DEPENDENT(str))
  115. #define JSSTRING_IS_MUTABLE(str) (((str)->length & (JSSTRFLAG_DEPENDENT | \
  116. JSSTRFLAG_MUTABLE)) == \
  117. JSSTRFLAG_MUTABLE)
  118. #define JSSTRING_IS_ATOMIZED(str) (((str)->length & (JSSTRFLAG_DEPENDENT | \
  119. JSSTRFLAG_ATOMIZED)) ==\
  120. JSSTRFLAG_ATOMIZED)
  121. #define JSSTRING_CHARS(str) (JSSTRING_IS_DEPENDENT(str) \
  122. ? JSSTRDEP_CHARS(str) \
  123. : JSFLATSTR_CHARS(str))
  124. #define JSSTRING_LENGTH(str) (JSSTRING_IS_DEPENDENT(str) \
  125. ? JSSTRDEP_LENGTH(str) \
  126. : JSFLATSTR_LENGTH(str))
  127. #define JSSTRING_CHARS_AND_LENGTH(str, chars_, length_) \
  128. ((void)(JSSTRING_IS_DEPENDENT(str) \
  129. ? ((length_) = JSSTRDEP_LENGTH(str), \
  130. (chars_) = JSSTRDEP_CHARS(str)) \
  131. : ((length_) = JSFLATSTR_LENGTH(str), \
  132. (chars_) = JSFLATSTR_CHARS(str))))
  133. #define JSSTRING_CHARS_AND_END(str, chars_, end) \
  134. ((void)((end) = JSSTRING_IS_DEPENDENT(str) \
  135. ? JSSTRDEP_LENGTH(str) + ((chars_) = JSSTRDEP_CHARS(str)) \
  136. : JSFLATSTR_LENGTH(str) + ((chars_) = JSFLATSTR_CHARS(str))))
  137. /* Specific flat string initializer and accessor macros. */
  138. #define JSFLATSTR_INIT(str, chars_, length_) \
  139. ((void)(JS_ASSERT(((length_) & ~JSSTRING_LENGTH_MASK) == 0), \
  140. (str)->length = (length_), (str)->u.chars = (chars_)))
  141. #define JSFLATSTR_LENGTH(str) \
  142. (JS_ASSERT(JSSTRING_IS_FLAT(str)), (str)->length & JSSTRING_LENGTH_MASK)
  143. #define JSFLATSTR_CHARS(str) \
  144. (JS_ASSERT(JSSTRING_IS_FLAT(str)), (str)->u.chars)
  145. /*
  146. * Macros to manipulate atomized and mutable flags of flat strings. It is safe
  147. * to use these without extra locking due to the following properties:
  148. *
  149. * * We do not have a macro like JSFLATSTR_CLEAR_ATOMIZED as a string
  150. * remains atomized until the GC collects it.
  151. *
  152. * * A thread may call JSFLATSTR_SET_MUTABLE only when it is the only thread
  153. * accessing the string until a later call to JSFLATSTR_CLEAR_MUTABLE.
  154. *
  155. * * Multiple threads can call JSFLATSTR_CLEAR_MUTABLE but the macro
  156. * actually clears the mutable flag only when the flag is set -- in which
  157. * case only one thread can access the string (see previous property).
  158. *
  159. * Thus, when multiple threads access the string, JSFLATSTR_SET_ATOMIZED is
  160. * the only macro that can update the length field of the string by changing
  161. * the mutable bit from 0 to 1. We call the macro only after the string has
  162. * been hashed. When some threads in js_ValueToStringId see that the flag is
  163. * set, it knows that the string was atomized.
  164. *
  165. * On the other hand, if the thread sees that the flag is unset, it could be
  166. * seeing a stale value when another thread has just atomized the string and
  167. * set the flag. But this can lead only to an extra call to js_AtomizeString.
  168. * This function would find that the string was already hashed and return it
  169. * with the atomized bit set.
  170. */
  171. #define JSFLATSTR_SET_ATOMIZED(str) \
  172. ((void)(JS_ASSERT(JSSTRING_IS_FLAT(str) && !JSSTRING_IS_MUTABLE(str)), \
  173. (str)->length |= JSSTRFLAG_ATOMIZED))
  174. #define JSFLATSTR_SET_MUTABLE(str) \
  175. ((void)(JS_ASSERT(JSSTRING_IS_FLAT(str) && !JSSTRING_IS_ATOMIZED(str)), \
  176. (str)->length |= JSSTRFLAG_MUTABLE))
  177. #define JSFLATSTR_CLEAR_MUTABLE(str) \
  178. ((void)(JS_ASSERT(JSSTRING_IS_FLAT(str)), \
  179. JSSTRING_HAS_FLAG(str, JSSTRFLAG_MUTABLE) && \
  180. ((str)->length &= ~JSSTRFLAG_MUTABLE)))
  181. /* Specific dependent string shift/mask accessor and mutator macros. */
  182. #define JSSTRDEP_START_BITS (JSSTRING_LENGTH_BITS-JSSTRDEP_LENGTH_BITS)
  183. #define JSSTRDEP_START_SHIFT JSSTRDEP_LENGTH_BITS
  184. #define JSSTRDEP_START_MASK JSSTRING_BITMASK(JSSTRDEP_START_BITS)
  185. #define JSSTRDEP_LENGTH_BITS (JSSTRING_LENGTH_BITS / 2)
  186. #define JSSTRDEP_LENGTH_MASK JSSTRING_BITMASK(JSSTRDEP_LENGTH_BITS)
  187. #define JSSTRDEP_IS_PREFIX(str) JSSTRING_HAS_FLAG(str, JSSTRFLAG_PREFIX)
  188. #define JSSTRDEP_START(str) (JSSTRDEP_IS_PREFIX(str) ? 0 \
  189. : (((str)->length \
  190. >> JSSTRDEP_START_SHIFT) \
  191. & JSSTRDEP_START_MASK))
  192. #define JSSTRDEP_LENGTH(str) ((str)->length \
  193. & (JSSTRDEP_IS_PREFIX(str) \
  194. ? JSSTRING_LENGTH_MASK \
  195. : JSSTRDEP_LENGTH_MASK))
  196. #define JSSTRDEP_INIT(str,bstr,off,len) \
  197. ((str)->length = JSSTRFLAG_DEPENDENT \
  198. | ((off) << JSSTRDEP_START_SHIFT) \
  199. | (len), \
  200. (str)->u.base = (bstr))
  201. #define JSPREFIX_INIT(str,bstr,len) \
  202. ((str)->length = JSSTRFLAG_DEPENDENT | JSSTRFLAG_PREFIX | (len), \
  203. (str)->u.base = (bstr))
  204. #define JSSTRDEP_BASE(str) ((str)->u.base)
  205. #define JSPREFIX_BASE(str) JSSTRDEP_BASE(str)
  206. #define JSPREFIX_SET_BASE(str,bstr) ((str)->u.base = (bstr))
  207. #define JSSTRDEP_CHARS(str) \
  208. (JSSTRING_IS_DEPENDENT(JSSTRDEP_BASE(str)) \
  209. ? js_GetDependentStringChars(str) \
  210. : JSFLATSTR_CHARS(JSSTRDEP_BASE(str)) + JSSTRDEP_START(str))
  211. extern size_t
  212. js_MinimizeDependentStrings(JSString *str, int level, JSString **basep);
  213. extern jschar *
  214. js_GetDependentStringChars(JSString *str);
  215. extern const jschar *
  216. js_GetStringChars(JSContext *cx, JSString *str);
  217. extern JSString * JS_FASTCALL
  218. js_ConcatStrings(JSContext *cx, JSString *left, JSString *right);
  219. extern const jschar *
  220. js_UndependString(JSContext *cx, JSString *str);
  221. extern JSBool
  222. js_MakeStringImmutable(JSContext *cx, JSString *str);
  223. extern JSString* JS_FASTCALL
  224. js_toLowerCase(JSContext *cx, JSString *str);
  225. extern JSString* JS_FASTCALL
  226. js_toUpperCase(JSContext *cx, JSString *str);
  227. typedef struct JSCharBuffer {
  228. size_t length;
  229. jschar *chars;
  230. } JSCharBuffer;
  231. struct JSSubString {
  232. size_t length;
  233. const jschar *chars;
  234. };
  235. extern jschar js_empty_ucstr[];
  236. extern JSSubString js_EmptySubString;
  237. /* Unicode character attribute lookup tables. */
  238. extern const uint8 js_X[];
  239. extern const uint8 js_Y[];
  240. extern const uint32 js_A[];
  241. /* Enumerated Unicode general category types. */
  242. typedef enum JSCharType {
  243. JSCT_UNASSIGNED = 0,
  244. JSCT_UPPERCASE_LETTER = 1,
  245. JSCT_LOWERCASE_LETTER = 2,
  246. JSCT_TITLECASE_LETTER = 3,
  247. JSCT_MODIFIER_LETTER = 4,
  248. JSCT_OTHER_LETTER = 5,
  249. JSCT_NON_SPACING_MARK = 6,
  250. JSCT_ENCLOSING_MARK = 7,
  251. JSCT_COMBINING_SPACING_MARK = 8,
  252. JSCT_DECIMAL_DIGIT_NUMBER = 9,
  253. JSCT_LETTER_NUMBER = 10,
  254. JSCT_OTHER_NUMBER = 11,
  255. JSCT_SPACE_SEPARATOR = 12,
  256. JSCT_LINE_SEPARATOR = 13,
  257. JSCT_PARAGRAPH_SEPARATOR = 14,
  258. JSCT_CONTROL = 15,
  259. JSCT_FORMAT = 16,
  260. JSCT_PRIVATE_USE = 18,
  261. JSCT_SURROGATE = 19,
  262. JSCT_DASH_PUNCTUATION = 20,
  263. JSCT_START_PUNCTUATION = 21,
  264. JSCT_END_PUNCTUATION = 22,
  265. JSCT_CONNECTOR_PUNCTUATION = 23,
  266. JSCT_OTHER_PUNCTUATION = 24,
  267. JSCT_MATH_SYMBOL = 25,
  268. JSCT_CURRENCY_SYMBOL = 26,
  269. JSCT_MODIFIER_SYMBOL = 27,
  270. JSCT_OTHER_SYMBOL = 28
  271. } JSCharType;
  272. /* Character classifying and mapping macros, based on java.lang.Character. */
  273. #define JS_CCODE(c) (js_A[js_Y[(js_X[(uint16)(c)>>6]<<6)|((c)&0x3F)]])
  274. #define JS_CTYPE(c) (JS_CCODE(c) & 0x1F)
  275. #define JS_ISALPHA(c) ((((1 << JSCT_UPPERCASE_LETTER) | \
  276. (1 << JSCT_LOWERCASE_LETTER) | \
  277. (1 << JSCT_TITLECASE_LETTER) | \
  278. (1 << JSCT_MODIFIER_LETTER) | \
  279. (1 << JSCT_OTHER_LETTER)) \
  280. >> JS_CTYPE(c)) & 1)
  281. #define JS_ISALNUM(c) ((((1 << JSCT_UPPERCASE_LETTER) | \
  282. (1 << JSCT_LOWERCASE_LETTER) | \
  283. (1 << JSCT_TITLECASE_LETTER) | \
  284. (1 << JSCT_MODIFIER_LETTER) | \
  285. (1 << JSCT_OTHER_LETTER) | \
  286. (1 << JSCT_DECIMAL_DIGIT_NUMBER)) \
  287. >> JS_CTYPE(c)) & 1)
  288. /* A unicode letter, suitable for use in an identifier. */
  289. #define JS_ISLETTER(c) ((((1 << JSCT_UPPERCASE_LETTER) | \
  290. (1 << JSCT_LOWERCASE_LETTER) | \
  291. (1 << JSCT_TITLECASE_LETTER) | \
  292. (1 << JSCT_MODIFIER_LETTER) | \
  293. (1 << JSCT_OTHER_LETTER) | \
  294. (1 << JSCT_LETTER_NUMBER)) \
  295. >> JS_CTYPE(c)) & 1)
  296. /*
  297. * 'IdentifierPart' from ECMA grammar, is Unicode letter or combining mark or
  298. * digit or connector punctuation.
  299. */
  300. #define JS_ISIDPART(c) ((((1 << JSCT_UPPERCASE_LETTER) | \
  301. (1 << JSCT_LOWERCASE_LETTER) | \
  302. (1 << JSCT_TITLECASE_LETTER) | \
  303. (1 << JSCT_MODIFIER_LETTER) | \
  304. (1 << JSCT_OTHER_LETTER) | \
  305. (1 << JSCT_LETTER_NUMBER) | \
  306. (1 << JSCT_NON_SPACING_MARK) | \
  307. (1 << JSCT_COMBINING_SPACING_MARK) | \
  308. (1 << JSCT_DECIMAL_DIGIT_NUMBER) | \
  309. (1 << JSCT_CONNECTOR_PUNCTUATION)) \
  310. >> JS_CTYPE(c)) & 1)
  311. /* Unicode control-format characters, ignored in input */
  312. #define JS_ISFORMAT(c) (((1 << JSCT_FORMAT) >> JS_CTYPE(c)) & 1)
  313. /*
  314. * Per ECMA-262 15.10.2.6, these characters are the only ones that make up a
  315. * "word", as far as a RegExp is concerned. If we want a Unicode-friendlier
  316. * definition of "word", we should rename this macro to something regexp-y.
  317. */
  318. #define JS_ISWORD(c) ((c) < 128 && (isalnum(c) || (c) == '_'))
  319. #define JS_ISIDSTART(c) (JS_ISLETTER(c) || (c) == '_' || (c) == '$')
  320. #define JS_ISIDENT(c) (JS_ISIDPART(c) || (c) == '_' || (c) == '$')
  321. #define JS_ISXMLSPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\r' || \
  322. (c) == '\n')
  323. #define JS_ISXMLNSSTART(c) ((JS_CCODE(c) & 0x00000100) || (c) == '_')
  324. #define JS_ISXMLNS(c) ((JS_CCODE(c) & 0x00000080) || (c) == '.' || \
  325. (c) == '-' || (c) == '_')
  326. #define JS_ISXMLNAMESTART(c) (JS_ISXMLNSSTART(c) || (c) == ':')
  327. #define JS_ISXMLNAME(c) (JS_ISXMLNS(c) || (c) == ':')
  328. #define JS_ISDIGIT(c) (JS_CTYPE(c) == JSCT_DECIMAL_DIGIT_NUMBER)
  329. /* XXXbe unify on A/X/Y tbls, avoid ctype.h? */
  330. /* XXXbe fs, etc. ? */
  331. #define JS_ISSPACE(c) ((JS_CCODE(c) & 0x00070000) == 0x00040000)
  332. #define JS_ISPRINT(c) ((c) < 128 && isprint(c))
  333. #define JS_ISUPPER(c) (JS_CTYPE(c) == JSCT_UPPERCASE_LETTER)
  334. #define JS_ISLOWER(c) (JS_CTYPE(c) == JSCT_LOWERCASE_LETTER)
  335. #define JS_TOUPPER(c) ((jschar) ((JS_CCODE(c) & 0x00100000) \
  336. ? (c) - ((int32)JS_CCODE(c) >> 22) \
  337. : (c)))
  338. #define JS_TOLOWER(c) ((jschar) ((JS_CCODE(c) & 0x00200000) \
  339. ? (c) + ((int32)JS_CCODE(c) >> 22) \
  340. : (c)))
  341. /*
  342. * Shorthands for ASCII (7-bit) decimal and hex conversion.
  343. * Manually inline isdigit for performance; MSVC doesn't do this for us.
  344. */
  345. #define JS7_ISDEC(c) ((((unsigned)(c)) - '0') <= 9)
  346. #define JS7_UNDEC(c) ((c) - '0')
  347. #define JS7_ISHEX(c) ((c) < 128 && isxdigit(c))
  348. #define JS7_UNHEX(c) (uintN)(JS7_ISDEC(c) ? (c) - '0' : 10 + tolower(c) - 'a')
  349. #define JS7_ISLET(c) ((c) < 128 && isalpha(c))
  350. /* Initialize per-runtime string state for the first context in the runtime. */
  351. extern JSBool
  352. js_InitRuntimeStringState(JSContext *cx);
  353. extern JSBool
  354. js_InitDeflatedStringCache(JSRuntime *rt);
  355. /*
  356. * Maximum character code for which we will create a pinned unit string on
  357. * demand -- see JSRuntime.unitStrings in jscntxt.h.
  358. */
  359. #define UNIT_STRING_LIMIT 256U
  360. /*
  361. * Get the independent string containing only character code at index in str
  362. * (backstopped with a zero character as usual for independent strings).
  363. */
  364. extern JSString *
  365. js_GetUnitString(JSContext *cx, JSString *str, size_t index);
  366. /*
  367. * Get the independent string containing only the character code c, which must
  368. * be less than UNIT_STRING_LIMIT.
  369. */
  370. extern JSString *
  371. js_GetUnitStringForChar(JSContext *cx, jschar c);
  372. extern void
  373. js_FinishUnitStrings(JSRuntime *rt);
  374. extern void
  375. js_FinishRuntimeStringState(JSContext *cx);
  376. extern void
  377. js_FinishDeflatedStringCache(JSRuntime *rt);
  378. /* Initialize the String class, returning its prototype object. */
  379. extern JSClass js_StringClass;
  380. extern JSObject *
  381. js_InitStringClass(JSContext *cx, JSObject *obj);
  382. extern const char js_escape_str[];
  383. extern const char js_unescape_str[];
  384. extern const char js_uneval_str[];
  385. extern const char js_decodeURI_str[];
  386. extern const char js_encodeURI_str[];
  387. extern const char js_decodeURIComponent_str[];
  388. extern const char js_encodeURIComponent_str[];
  389. /* GC-allocate a string descriptor for the given malloc-allocated chars. */
  390. extern JSString *
  391. js_NewString(JSContext *cx, jschar *chars, size_t length);
  392. extern JSString *
  393. js_NewDependentString(JSContext *cx, JSString *base, size_t start,
  394. size_t length);
  395. /* Copy a counted string and GC-allocate a descriptor for it. */
  396. extern JSString *
  397. js_NewStringCopyN(JSContext *cx, const jschar *s, size_t n);
  398. /* Copy a C string and GC-allocate a descriptor for it. */
  399. extern JSString *
  400. js_NewStringCopyZ(JSContext *cx, const jschar *s);
  401. /*
  402. * Free the chars held by str when it is finalized by the GC. When type is
  403. * less then zero, it denotes an internal string. Otherwise it denotes the
  404. * type of the external string allocated with JS_NewExternalString.
  405. *
  406. * This function always needs rt but can live with null cx.
  407. */
  408. extern void
  409. js_FinalizeStringRT(JSRuntime *rt, JSString *str, intN type, JSContext *cx);
  410. /*
  411. * Convert a value to a printable C string.
  412. */
  413. typedef JSString *(*JSValueToStringFun)(JSContext *cx, jsval v);
  414. extern JS_FRIEND_API(const char *)
  415. js_ValueToPrintable(JSContext *cx, jsval v, JSValueToStringFun v2sfun);
  416. #define js_ValueToPrintableString(cx,v) \
  417. js_ValueToPrintable(cx, v, js_ValueToString)
  418. #define js_ValueToPrintableSource(cx,v) \
  419. js_ValueToPrintable(cx, v, js_ValueToSource)
  420. /*
  421. * Convert a value to a string, returning null after reporting an error,
  422. * otherwise returning a new string reference.
  423. */
  424. extern JS_FRIEND_API(JSString *)
  425. js_ValueToString(JSContext *cx, jsval v);
  426. /*
  427. * Convert a value to its source expression, returning null after reporting
  428. * an error, otherwise returning a new string reference.
  429. */
  430. extern JS_FRIEND_API(JSString *)
  431. js_ValueToSource(JSContext *cx, jsval v);
  432. /*
  433. * Compute a hash function from str. The caller can call this function even if
  434. * str is not a GC-allocated thing.
  435. */
  436. extern uint32
  437. js_HashString(JSString *str);
  438. /*
  439. * Test if strings are equal. The caller can call the function even if str1
  440. * or str2 are not GC-allocated things.
  441. */
  442. extern JSBool JS_FASTCALL
  443. js_EqualStrings(JSString *str1, JSString *str2);
  444. /*
  445. * Return less than, equal to, or greater than zero depending on whether
  446. * str1 is less than, equal to, or greater than str2.
  447. */
  448. extern int32 JS_FASTCALL
  449. js_CompareStrings(JSString *str1, JSString *str2);
  450. /*
  451. * Boyer-Moore-Horspool superlinear search for pat:patlen in text:textlen.
  452. * The patlen argument must be positive and no greater than BMH_PATLEN_MAX.
  453. * The start argument tells where in text to begin the search.
  454. *
  455. * Return the index of pat in text, or -1 if not found.
  456. */
  457. #define BMH_CHARSET_SIZE 256 /* ISO-Latin-1 */
  458. #define BMH_PATLEN_MAX 255 /* skip table element is uint8 */
  459. #define BMH_BAD_PATTERN (-2) /* return value if pat is not ISO-Latin-1 */
  460. extern jsint
  461. js_BoyerMooreHorspool(const jschar *text, jsint textlen,
  462. const jschar *pat, jsint patlen,
  463. jsint start);
  464. extern size_t
  465. js_strlen(const jschar *s);
  466. extern jschar *
  467. js_strchr(const jschar *s, jschar c);
  468. extern jschar *
  469. js_strchr_limit(const jschar *s, jschar c, const jschar *limit);
  470. #define js_strncpy(t, s, n) memcpy((t), (s), (n) * sizeof(jschar))
  471. /*
  472. * Return s advanced past any Unicode white space characters.
  473. */
  474. extern const jschar *
  475. js_SkipWhiteSpace(const jschar *s, const jschar *end);
  476. /*
  477. * Inflate bytes to JS chars and vice versa. Report out of memory via cx
  478. * and return null on error, otherwise return the jschar or byte vector that
  479. * was JS_malloc'ed. length is updated with the length of the new string in jschars.
  480. */
  481. extern jschar *
  482. js_InflateString(JSContext *cx, const char *bytes, size_t *length);
  483. extern char *
  484. js_DeflateString(JSContext *cx, const jschar *chars, size_t length);
  485. /*
  486. * Inflate bytes to JS chars into a buffer. 'chars' must be large enough for
  487. * 'length' jschars. The buffer is NOT null-terminated. The destination length
  488. * must be be initialized with the buffer size and will contain on return the
  489. * number of copied chars.
  490. */
  491. extern JSBool
  492. js_InflateStringToBuffer(JSContext* cx, const char *bytes, size_t length,
  493. jschar *chars, size_t* charsLength);
  494. /*
  495. * Get number of bytes in the deflated sequence of characters.
  496. */
  497. extern size_t
  498. js_GetDeflatedStringLength(JSContext *cx, const jschar *chars,
  499. size_t charsLength);
  500. /*
  501. * Deflate JS chars to bytes into a buffer. 'bytes' must be large enough for
  502. * 'length chars. The buffer is NOT null-terminated. The destination length
  503. * must to be initialized with the buffer size and will contain on return the
  504. * number of copied bytes.
  505. */
  506. extern JSBool
  507. js_DeflateStringToBuffer(JSContext* cx, const jschar *chars,
  508. size_t charsLength, char *bytes, size_t* length);
  509. /*
  510. * Associate bytes with str in the deflated string cache, returning true on
  511. * successful association, false on out of memory.
  512. */
  513. extern JSBool
  514. js_SetStringBytes(JSContext *cx, JSString *str, char *bytes, size_t length);
  515. /*
  516. * Find or create a deflated string cache entry for str that contains its
  517. * characters chopped from Unicode code points into bytes.
  518. */
  519. extern const char *
  520. js_GetStringBytes(JSContext *cx, JSString *str);
  521. /* Remove a deflated string cache entry associated with str if any. */
  522. extern void
  523. js_PurgeDeflatedStringCache(JSRuntime *rt, JSString *str);
  524. /* Export a few natives and a helper to other files in SpiderMonkey. */
  525. extern JSBool
  526. js_str_escape(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
  527. jsval *rval);
  528. extern JSBool
  529. js_StringMatchHelper(JSContext *cx, uintN argc, jsval *vp, jsbytecode *pc);
  530. extern JSBool
  531. js_StringReplaceHelper(JSContext *cx, uintN argc, JSObject *lambda,
  532. JSString *repstr, jsval *vp);
  533. /*
  534. * Convert one UCS-4 char and write it into a UTF-8 buffer, which must be at
  535. * least 6 bytes long. Return the number of UTF-8 bytes of data written.
  536. */
  537. extern int
  538. js_OneUcs4ToUtf8Char(uint8 *utf8Buffer, uint32 ucs4Char);
  539. /*
  540. * Write str into buffer escaping any non-printable or non-ASCII character.
  541. * Guarantees that a NUL is at the end of the buffer. Returns the length of
  542. * the written output, NOT including the NUL. If buffer is null, just returns
  543. * the length of the output. If quote is not 0, it must be a single or double
  544. * quote character that will quote the output.
  545. *
  546. * The function is only defined for debug builds.
  547. */
  548. #define js_PutEscapedString(buffer, bufferSize, str, quote) \
  549. js_PutEscapedStringImpl(buffer, bufferSize, NULL, str, quote)
  550. /*
  551. * Write str into file escaping any non-printable or non-ASCII character.
  552. * Returns the number of bytes written to file. If quote is not 0, it must
  553. * be a single or double quote character that will quote the output.
  554. *
  555. * The function is only defined for debug builds.
  556. */
  557. #define js_FileEscapedString(file, str, quote) \
  558. (JS_ASSERT(file), js_PutEscapedStringImpl(NULL, 0, file, str, quote))
  559. extern JS_FRIEND_API(size_t)
  560. js_PutEscapedStringImpl(char *buffer, size_t bufferSize, FILE *fp,
  561. JSString *str, uint32 quote);
  562. JS_END_EXTERN_C
  563. #endif /* jsstr_h___ */