PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/antlr-3.0/runtime/C/include/antlr3string.h

http://perseph.googlecode.com/
C Header | 227 lines | 51 code | 41 blank | 135 comment | 0 complexity | 147e0bb1a25e356fbbe20b7e76726c87 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. /** \file
  2. * Simple string interface allows indiscriminate allocation of strings
  3. * such that they can be allocated all over the place and released in
  4. * one chunk via a string factory - saves lots of hassle in remmebering what
  5. * strings were allocated where.
  6. */
  7. #ifndef _ANTLR3_STRING_H
  8. #define _ANTLR3_STRING_H
  9. #include <antlr3defs.h>
  10. #include <antlr3collections.h>
  11. /** Base string class tracks the allocations and provides simple string
  12. * tracking functions. Mostly you can work directly on the string for things
  13. * that don't reallocate it, like strchr() etc. Perhaps someone will want to provide implementations for UTF8
  14. * and so on.
  15. */
  16. typedef struct ANTLR3_STRING_struct
  17. {
  18. /** The factory that created this string
  19. */
  20. pANTLR3_STRING_FACTORY factory;
  21. /** Pointer to the current string value (starts at NULL unless
  22. * the string allocator is told to create it with a preknown size.
  23. */
  24. pANTLR3_UINT8 chars;
  25. /** Current length of the string up to and not including, the trailing '\0'
  26. * Note that the actual allocation (->size)
  27. * is always at least one byte more than this to accomodate trailing '\0'
  28. */
  29. ANTLR3_UINT32 len;
  30. /** Current size of the string in bytes including the trailing '\0'
  31. */
  32. ANTLR3_UINT32 size;
  33. /** Index of string (allocation number) in case someone wants
  34. * to explictly release it.
  35. */
  36. ANTLR3_UINT32 index;
  37. /** Occasionally it is useful to know what the encoding of the string
  38. * actually is, hence it is stored here as one the ANTLR3_ENCODING_ values
  39. */
  40. ANTLR3_UINT8 encoding;
  41. /** Pointer to function that sets the string value to a specific string in the default encoding
  42. * for this string. For instance, if this is ASCII 8 bit, then this function is the same as set8
  43. * but if the encoding is 16 bit, then the pointer is assumed to point to 16 bit characters not
  44. * 8 bit.
  45. */
  46. pANTLR3_UINT8 (*set) (struct ANTLR3_STRING_struct * string, const char * chars);
  47. /** Pointer to function that sets the string value to a specific 8 bit string in the default encoding
  48. * for this string. For instance, if this is a 16 bit string, then this function is the same as set8
  49. * but if the encoding is 16 bit, then the pointer is assumed to point to 8 bit characters that must
  50. * be converted to 16 bit characters on the fly.
  51. */
  52. pANTLR3_UINT8 (*set8) (struct ANTLR3_STRING_struct * string, const char * chars);
  53. /** Pointer to function adds a raw char * type pointer in the default encoding
  54. * for this string. For instance, if this is ASCII 8 bit, then this function is the same as append8
  55. * but if the encoding is 16 bit, then the pointer is assumed to point to 16 bit characters not
  56. * 8 bit.
  57. */
  58. pANTLR3_UINT8 (*append) (struct ANTLR3_STRING_struct * string, const char * newbit);
  59. /** Pointer to function adds a raw char * type pointer in the default encoding
  60. * for this string. For instance, if this is a 16 bit string, then this function assumes the pointer
  61. * points to 8 bit characters that must be converted on the fly.
  62. */
  63. pANTLR3_UINT8 (*append8) (struct ANTLR3_STRING_struct * string, const char * newbit);
  64. /** Pointer to function that inserts the supplied string at the specified
  65. * offset in the current string in the default encoding for this string. For instance, if this is an 8
  66. * bit string, then this is the same as insert8, but if this is a 16 bit string, then the poitner
  67. * must point to 16 bit characters.
  68. *
  69. */
  70. pANTLR3_UINT8 (*insert) (struct ANTLR3_STRING_struct * string, ANTLR3_UINT32 point, const char * newbit);
  71. /** Pointer to function that inserts the supplied string at the specified
  72. * offset in the current string in the default encoding for this string. For instance, if this is a 16 bit string
  73. * then the pointer is assumed to point at 8 bit characteres that must be converted on the fly.
  74. */
  75. pANTLR3_UINT8 (*insert8) (struct ANTLR3_STRING_struct * string, ANTLR3_UINT32 point, const char * newbit);
  76. /** Pointer to function that sets the string value to a copy of the supplied string (strings must be in the
  77. * same encoding.
  78. */
  79. pANTLR3_UINT8 (*setS) (struct ANTLR3_STRING_struct * string, struct ANTLR3_STRING_struct * chars);
  80. /** Pointer to function appends a copy of the characters contained in another string. Strings must be in the
  81. * same encoding.
  82. */
  83. pANTLR3_UINT8 (*appendS) (struct ANTLR3_STRING_struct * string, struct ANTLR3_STRING_struct * newbit);
  84. /** Pointer to function that inserts a copy of the characters in the supplied string at the specified
  85. * offset in the current string. strings must be in the same encoding.
  86. */
  87. pANTLR3_UINT8 (*insertS) (struct ANTLR3_STRING_struct * string, ANTLR3_UINT32 point, struct ANTLR3_STRING_struct * newbit);
  88. /** Pointer to function that inserts the supplied integer in string form at the specified
  89. * offset in the current string.
  90. */
  91. pANTLR3_UINT8 (*inserti) (struct ANTLR3_STRING_struct * string, ANTLR3_UINT32 point, ANTLR3_INT32 i);
  92. /** Pointer to function that adds a single character to the end of the string, in the encoding of the
  93. * string - 8 bit, 16 bit, utf-8 etc. Input is a single UTF32 (32 bits wide integer) character.
  94. */
  95. pANTLR3_UINT8 (*addc) (struct ANTLR3_STRING_struct * string, ANTLR3_UINT32 c);
  96. /** Pointer to function that adds the stringified representation of an integer
  97. * to the string.
  98. */
  99. pANTLR3_UINT8 (*addi) (struct ANTLR3_STRING_struct * string, ANTLR3_INT32 i);
  100. /** Pointer to function that compares the text of a string to the supplied
  101. * 8 bit character string and returns a result a la strcmp()
  102. */
  103. ANTLR3_UINT32 (*compare8) (struct ANTLR3_STRING_struct * string, const char * compStr);
  104. /** Pointer to a function that compares the text of a string with the supplied character string
  105. * (which is assumed to be in the same encoding as the string itself) and returns a result
  106. * a la strcmp()
  107. */
  108. ANTLR3_UINT32 (*compare) (struct ANTLR3_STRING_struct * string, const char * compStr);
  109. /** Pointer to a function that compares the text of a string with the supplied string
  110. * (which is assumed to be in the same encoding as the string itself) and returns a result
  111. * a la strcmp()
  112. */
  113. ANTLR3_UINT32 (*compareS) (struct ANTLR3_STRING_struct * string, struct ANTLR3_STRING_struct * compStr);
  114. /** Pointer to a function that returns the character indexed at the supplied
  115. * offset as a 32 bit character.
  116. */
  117. ANTLR3_UCHAR (*charAt) (struct ANTLR3_STRING_struct * string, ANTLR3_UINT32 offset);
  118. /** Pointer to a function that returns a substring of the supplied string a la .subString(s,e)
  119. * in java runtimes.
  120. */
  121. struct ANTLR3_STRING_struct *
  122. (*subString) (struct ANTLR3_STRING_struct * string, ANTLR3_UINT32 startIndex, ANTLR3_UINT32 endIndex);
  123. /** Pointer to a function that returns the integer representation of any numeric characters
  124. * at the beginning of the string
  125. */
  126. ANTLR3_INT32 (*toInt32) (struct ANTLR3_STRING_struct * string);
  127. /** Pointer to a function that yields an 8 bit string regardless of the encoding of the supplied
  128. * string. This is useful when you want to use the text of a token in some way that requires an 8 bit
  129. * value, such as the key for a hashtable. The function is required to produce a usable string even
  130. * if the text given as input has characters that do not fit in 8 bit space, it will replace them
  131. * with some arbitrary character such as '?'
  132. */
  133. struct ANTLR3_STRING_struct *
  134. (*to8) (struct ANTLR3_STRING_struct * string);
  135. }
  136. ANTLR3_STRING;
  137. /** Definition of the string factory interface, which creates and tracks
  138. * strings for you of various shapes and sizes.
  139. */
  140. typedef struct ANTLR3_STRING_FACTORY_struct
  141. {
  142. /** List of all the strings that have been allocated by the factory
  143. */
  144. pANTLR3_VECTOR strings;
  145. /* Index of next string that we allocate
  146. */
  147. ANTLR3_UINT32 index;
  148. /** Pointer to function that manufactures an empty string
  149. */
  150. pANTLR3_STRING (*newRaw) (struct ANTLR3_STRING_FACTORY_struct * factory);
  151. /** Pointer to function that manufactures a raw string with no text in it but space for size
  152. * characters.
  153. */
  154. pANTLR3_STRING (*newSize) (struct ANTLR3_STRING_FACTORY_struct * factory, ANTLR3_UINT32 size);
  155. /** Pointer to function that manufactures a string from a given pointer and length. The pointer is assumed
  156. * to point to characters in the same encoding as the string type, hence if this is a 16 bit string the
  157. * pointer should point to 16 bit characters.
  158. */
  159. pANTLR3_STRING (*newPtr) (struct ANTLR3_STRING_FACTORY_struct * factory, pANTLR3_UINT8 string, ANTLR3_UINT32 size);
  160. /** Pointer to function that manufactures a string from a given pointer and length. The pointer is assumed to
  161. * point at 8 bit characters which must be converted on the fly to the encoding of the actual string.
  162. */
  163. pANTLR3_STRING (*newPtr8) (struct ANTLR3_STRING_FACTORY_struct * factory, pANTLR3_UINT8 string, ANTLR3_UINT32 size);
  164. /** Pointer to function that manufactures a string from a given pointer and works out the length. The pointer is
  165. * assumed to point to characters in the same encoding as the string itself, i.e. 16 bit if a 16 bit
  166. * string and so on.
  167. */
  168. pANTLR3_STRING (*newStr) (struct ANTLR3_STRING_FACTORY_struct * factory, pANTLR3_UINT8 string);
  169. /** Pointer to function that manufactures a string from a given pointer and length. The pointer should
  170. * point to 8 bit characters regardless of the actual encoding of the string. The 8 bit characters
  171. * will be converted to the actual string encoding on the fly.
  172. */
  173. pANTLR3_STRING (*newStr8) (struct ANTLR3_STRING_FACTORY_struct * factory, pANTLR3_UINT8 string);
  174. /** Pointer to function that deletes the string altogether
  175. */
  176. void (*destroy) (struct ANTLR3_STRING_FACTORY_struct * factory, pANTLR3_STRING string);
  177. /** Pointer to function that returns a copy of the string in printable form without any control
  178. * characters in it.
  179. */
  180. pANTLR3_STRING (*printable)(struct ANTLR3_STRING_FACTORY_struct * factory, pANTLR3_STRING string);
  181. /** Pointer to function that closes the factory
  182. */
  183. void (*close) (struct ANTLR3_STRING_FACTORY_struct * factory);
  184. }
  185. ANTLR3_STRING_FACTORY;
  186. #endif