home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / js / src / jsstr.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.4 KB  |  249 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef jsstr_h___
  20. #define jsstr_h___
  21. /*
  22.  * JS string type implementation.
  23.  *
  24.  * A JS string is a counted array of unicode characters.  To support handoff
  25.  * of API client memory, the chars are allocated separately from the length,
  26.  * necessitating a pointer after the count, to form a string header.  String
  27.  * headers are GC'ed while string bytes are allocated from the malloc heap.
  28.  *
  29.  * When a string is treated as an object (by following it with . or []), the
  30.  * runtime wraps it with a JSObject whose valueOf method returns the unwrapped
  31.  * string header.
  32.  */
  33. #include <ctype.h>
  34. #include "jspubtd.h"
  35. #include "jsprvtd.h"
  36.  
  37. PR_BEGIN_EXTERN_C
  38.  
  39. struct JSString {
  40.     size_t          length;
  41.     jschar          *chars;
  42. };
  43.  
  44. struct JSSubString {
  45.     size_t          length;
  46.     const jschar    *chars;
  47. };
  48.  
  49. extern jschar      js_empty_ucstr[];
  50. extern JSSubString js_EmptySubString;
  51.  
  52. /* Unicode character attribute lookup tables. */
  53. extern const uint8 js_X[];
  54. extern const uint8 js_Y[];
  55. extern const int32 js_A[];
  56.  
  57. /* Enumerated Unicode general category types. */
  58. typedef enum JSCharType {
  59.     JSCT_UNASSIGNED             = 0,
  60.     JSCT_UPPERCASE_LETTER       = 1,
  61.     JSCT_LOWERCASE_LETTER       = 2,
  62.     JSCT_TITLECASE_LETTER       = 3,
  63.     JSCT_MODIFIER_LETTER        = 4,
  64.     JSCT_OTHER_LETTER           = 5,
  65.     JSCT_NON_SPACING_MARK       = 6,
  66.     JSCT_ENCLOSING_MARK         = 7,
  67.     JSCT_COMBINING_SPACING_MARK = 8,
  68.     JSCT_DECIMAL_DIGIT_NUMBER   = 9,
  69.     JSCT_LETTER_NUMBER          = 10,
  70.     JSCT_OTHER_NUMBER           = 11,
  71.     JSCT_SPACE_SEPARATOR        = 12,
  72.     JSCT_LINE_SEPARATOR         = 13,
  73.     JSCT_PARAGRAPH_SEPARATOR    = 14,
  74.     JSCT_CONTROL                = 15,
  75.     JSCT_FORMAT                 = 16,
  76.     JSCT_PRIVATE_USE            = 18,
  77.     JSCT_SURROGATE              = 19,
  78.     JSCT_DASH_PUNCTUATION       = 20,
  79.     JSCT_START_PUNCTUATION      = 21,
  80.     JSCT_END_PUNCTUATION        = 22,
  81.     JSCT_CONNECTOR_PUNCTUATION  = 23,
  82.     JSCT_OTHER_PUNCTUATION      = 24,
  83.     JSCT_MATH_SYMBOL            = 25,
  84.     JSCT_CURRENCY_SYMBOL        = 26,
  85.     JSCT_MODIFIER_SYMBOL        = 27,
  86.     JSCT_OTHER_SYMBOL           = 28
  87. } JSCharType;
  88.  
  89. /* Character classifying and mapping macros, based on java.lang.Character. */
  90. #define JS_CCODE(c)     (js_A[js_Y[(js_X[(c)>>6]<<6)|((c)&0x3F)]])
  91. #define JS_CTYPE(c)     (JS_CCODE(c) & 0x1F)
  92.  
  93. #define JS_ISALPHA(c)   ((((1 << JSCT_UPPERCASE_LETTER) |                     \
  94.                (1 << JSCT_LOWERCASE_LETTER) |                     \
  95.                (1 << JSCT_TITLECASE_LETTER) |                     \
  96.                (1 << JSCT_MODIFIER_LETTER) |                      \
  97.                (1 << JSCT_OTHER_LETTER))                          \
  98.               >> JS_CTYPE(c)) & 1)
  99.  
  100. #define JS_ISALNUM(c)   ((((1 << JSCT_UPPERCASE_LETTER) |                     \
  101.                (1 << JSCT_LOWERCASE_LETTER) |                     \
  102.                (1 << JSCT_TITLECASE_LETTER) |                     \
  103.                (1 << JSCT_MODIFIER_LETTER) |                      \
  104.                (1 << JSCT_OTHER_LETTER) |                         \
  105.                (1 << JSCT_DECIMAL_DIGIT_NUMBER))                  \
  106.               >> JS_CTYPE(c)) & 1)
  107.  
  108. #define JS_ISWORD(c)    (JS_ISALNUM(c) || (c) == '_')
  109.  
  110. /* XXXbe unify on A/X/Y tbls, avoid ctype.h? */
  111. #define JS_ISIDENT(c)   ((c) < 128 && (isalpha(c) || (c) == '_' || (c) == '$'))
  112. #define JS_ISIDENT2(c)  ((c) < 128 && (isalnum(c) || (c) == '_' || (c) == '$'))
  113.  
  114. #define JS_ISDIGIT(c)   (JS_CTYPE(c) == JSCT_DECIMAL_DIGIT_NUMBER)
  115.  
  116. /* XXXbe fs, etc. ? */
  117. #define JS_ISSPACE(c)   ((JS_CCODE(c) & 0x00070000) == 0x00040000)
  118. #define JS_ISPRINT(c)   ((c) < 128 && isprint(c))
  119.  
  120. #define JS_ISUPPER(c)   (JS_CTYPE(c) == JSCT_UPPERCASE_LETTER)
  121. #define JS_ISLOWER(c)   (JS_CTYPE(c) == JSCT_LOWERCASE_LETTER)
  122.  
  123. #ifdef __GNUC__
  124.  
  125. #define JS_TOUPPER(c)   ({int32 _v = JS_CCODE(c);                             \
  126.               (_v & 0x00100000) ? (c) - (_v >> 22) : (c);})
  127. #define JS_TOLOWER(c)   ({int32 _v = JS_CCODE(c);                             \
  128.               (_v & 0x00200000) ? (c) + (_v >> 22) : (c);})
  129.  
  130. #else  /* !__GNUC__ */
  131.  
  132. #define JS_TOUPPER(c)   js_ToUpper((jschar)c)
  133. #define JS_TOLOWER(c)   js_ToLower((jschar)c)
  134.  
  135. extern jschar js_ToUpper(jschar c);
  136. extern jschar js_ToLower(jschar c);
  137.  
  138. #endif /* !__GNUC__ */
  139.  
  140. #define JS_TOCTRL(c)    ((c) ^ 64)      /* XXX unsafe! requires uppercase c */
  141.  
  142. /* Shorthands for ASCII (7-bit) decimal and hex conversion. */
  143. #define JS7_ISDEC(c)    ((c) < 128 && isdigit(c))
  144. #define JS7_UNDEC(c)    ((c) - '0')
  145. #define JS7_ISHEX(c)    ((c) < 128 && isxdigit(c))
  146. #define JS7_UNHEX(c)    (uintN)(isdigit(c) ? (c) - '0' : 10 + tolower(c) - 'a')
  147. #define JS7_ISLET(c)    ((c) < 128 && isalpha(c))
  148.  
  149. /* Initialize the String class, returning its prototype object. */
  150. extern JSObject *
  151. js_InitStringClass(JSContext *cx, JSObject *obj);
  152.  
  153. /* GC-allocate a string descriptor for the given malloc-allocated chars. */
  154. extern JSString *
  155. js_NewString(JSContext *cx, jschar *chars, size_t length, uintN gcflag);
  156.  
  157. /* Copy a counted string and GC-allocate a descriptor for it. */
  158. extern JSString *
  159. js_NewStringCopyN(JSContext *cx, const jschar *s, size_t n, uintN gcflag);
  160.  
  161. /* Copy a C string and GC-allocate a descriptor for it. */
  162. extern JSString *
  163. js_NewStringCopyZ(JSContext *cx, const jschar *s, uintN gcflag);
  164.  
  165. /* Free the chars held by str when it is finalized by the GC. */
  166. extern void
  167. js_FinalizeString(JSContext *cx, JSString *str);
  168.  
  169. /* Wrap a string value in a String object. */
  170. extern JSObject *
  171. js_StringToObject(JSContext *cx, JSString *str);
  172.  
  173. /*
  174.  * Convert a value to a string, returning null after reporting an error,
  175.  * otherwise returning a new string reference.
  176.  */
  177. extern JSString *
  178. js_ValueToString(JSContext *cx, jsval v);
  179.  
  180. #ifdef HT_ENUMERATE_NEXT    /* XXX don't require prhash.h */
  181. /*
  182.  * Compute a hash function from str.
  183.  */
  184. extern PRHashNumber
  185. js_HashString(const JSString *str);
  186. #endif
  187.  
  188. /*
  189.  * Return less than, equal to, or greater than zero depending on whether
  190.  * str1 is less than, equal to, or greater than str2.
  191.  */
  192. extern intN
  193. js_CompareStrings(const JSString *str1, const JSString *str2);
  194.  
  195. /*
  196.  * Boyer-Moore-Horspool superlinear search for pat:patlen in text:textlen.
  197.  * The patlen argument must be positive and no greater than BMH_PATLEN_MAX.
  198.  * The start argument tells where in text to begin the search.
  199.  *
  200.  * Return the index of pat in text, or -1 if not found.
  201.  */
  202. #define BMH_CHARSET_SIZE 256    /* ISO-Latin-1 */
  203. #define BMH_PATLEN_MAX   255    /* skip table element is uint8 */
  204.  
  205. #define BMH_BAD_PATTERN  (-2)   /* return value if pat is not ISO-Latin-1 */
  206.  
  207. extern jsint
  208. js_BoyerMooreHorspool(const jschar *text, jsint textlen,
  209.               const jschar *pat, jsint patlen,
  210.               jsint start);
  211.  
  212. extern size_t
  213. js_strlen(const jschar *s);
  214.  
  215. extern jschar *
  216. js_strchr(const jschar *s, jschar c);
  217.  
  218. extern jschar *
  219. js_strncpy(jschar *t, const jschar *s, size_t n);
  220.  
  221. /*
  222.  * Inflate bytes to JS chars and vice versa.  Report out of memory via cx
  223.  * and return null on error, otherwise return the jschar or byte vector that
  224.  * was JS_malloc'ed.
  225.  */
  226. extern jschar *
  227. js_InflateString(JSContext *cx, const char *bytes, size_t length);
  228.  
  229. extern char *
  230. js_DeflateString(JSContext *cx, const jschar *chars, size_t length);
  231.  
  232. /*
  233.  * Associate bytes with str in the deflated string cache, returning true on
  234.  * successful association, false on out of memory.
  235.  */
  236. extern JSBool
  237. js_SetStringBytes(JSString *str, char *bytes, size_t length);
  238.  
  239. /*
  240.  * Find or create a deflated string cache entry for str that contains its
  241.  * characters chopped from Unicode code points into bytes.
  242.  */
  243. extern char *
  244. js_GetStringBytes(JSString *str);
  245.  
  246. PR_END_EXTERN_C
  247.  
  248. #endif /* jsstr_h___ */
  249.