]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/cpp/DataBoardTest/libantlr3c-3.2/src/antlr3string.c
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / cpp / DataBoardTest / libantlr3c-3.2 / src / antlr3string.c
1 /** \file
2  * Implementation of the ANTLR3 string and string factory classes
3  */
4
5 // [The "BSD licence"]
6 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
7 // http://www.temporal-wave.com
8 // http://www.linkedin.com/in/jimidle
9 //
10 // All rights reserved.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions
14 // are met:
15 // 1. Redistributions of source code must retain the above copyright
16 //    notice, this list of conditions and the following disclaimer.
17 // 2. Redistributions in binary form must reproduce the above copyright
18 //    notice, this list of conditions and the following disclaimer in the
19 //    documentation and/or other materials provided with the distribution.
20 // 3. The name of the author may not be used to endorse or promote products
21 //    derived from this software without specific prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34 #include    <antlr3string.h>
35
36 /* Factory API
37  */
38 static    pANTLR3_STRING    newRaw8     (pANTLR3_STRING_FACTORY factory);
39 static    pANTLR3_STRING    newRaw16    (pANTLR3_STRING_FACTORY factory);
40 static    pANTLR3_STRING    newSize8    (pANTLR3_STRING_FACTORY factory, ANTLR3_UINT32 size);
41 static    pANTLR3_STRING    newSize16   (pANTLR3_STRING_FACTORY factory, ANTLR3_UINT32 size);
42 static    pANTLR3_STRING    newPtr8     (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string, ANTLR3_UINT32 size);
43 static    pANTLR3_STRING    newPtr16_8  (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string, ANTLR3_UINT32 size);
44 static    pANTLR3_STRING    newPtr16_16 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string, ANTLR3_UINT32 size);
45 static    pANTLR3_STRING    newStr8     (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string);
46 static    pANTLR3_STRING    newStr16_8  (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string);
47 static    pANTLR3_STRING    newStr16_16 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string);
48 static    void              destroy     (pANTLR3_STRING_FACTORY factory, pANTLR3_STRING string);
49 static    pANTLR3_STRING    printable8  (pANTLR3_STRING_FACTORY factory, pANTLR3_STRING string);
50 static    pANTLR3_STRING    printable16 (pANTLR3_STRING_FACTORY factory, pANTLR3_STRING string);
51 static    void              closeFactory(pANTLR3_STRING_FACTORY factory);
52
53 /* String API
54  */
55 static    pANTLR3_UINT8     set8        (pANTLR3_STRING string, const char * chars);
56 static    pANTLR3_UINT8     set16_8     (pANTLR3_STRING string, const char * chars);
57 static    pANTLR3_UINT8     set16_16    (pANTLR3_STRING string, const char * chars);
58 static    pANTLR3_UINT8     append8     (pANTLR3_STRING string, const char * newbit);
59 static    pANTLR3_UINT8     append16_8  (pANTLR3_STRING string, const char * newbit);
60 static    pANTLR3_UINT8     append16_16 (pANTLR3_STRING string, const char * newbit);
61 static    pANTLR3_UINT8     insert8     (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit);
62 static    pANTLR3_UINT8     insert16_8  (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit);
63 static    pANTLR3_UINT8     insert16_16 (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit);
64
65 static    pANTLR3_UINT8     setS        (pANTLR3_STRING string, pANTLR3_STRING chars);
66 static    pANTLR3_UINT8     appendS     (pANTLR3_STRING string, pANTLR3_STRING newbit);
67 static    pANTLR3_UINT8     insertS     (pANTLR3_STRING string, ANTLR3_UINT32 point, pANTLR3_STRING newbit);
68
69 static    pANTLR3_UINT8     addc8       (pANTLR3_STRING string, ANTLR3_UINT32 c);
70 static    pANTLR3_UINT8     addc16      (pANTLR3_STRING string, ANTLR3_UINT32 c);
71 static    pANTLR3_UINT8     addi8       (pANTLR3_STRING string, ANTLR3_INT32 i);
72 static    pANTLR3_UINT8     addi16      (pANTLR3_STRING string, ANTLR3_INT32 i);
73 static    pANTLR3_UINT8     inserti8    (pANTLR3_STRING string, ANTLR3_UINT32 point, ANTLR3_INT32 i);
74 static    pANTLR3_UINT8     inserti16   (pANTLR3_STRING string, ANTLR3_UINT32 point, ANTLR3_INT32 i);
75
76 static    ANTLR3_UINT32     compare8    (pANTLR3_STRING string, const char * compStr);
77 static    ANTLR3_UINT32     compare16_8 (pANTLR3_STRING string, const char * compStr);
78 static    ANTLR3_UINT32     compare16_16(pANTLR3_STRING string, const char * compStr);
79 static    ANTLR3_UINT32     compareS    (pANTLR3_STRING string, pANTLR3_STRING compStr);
80 static    ANTLR3_UCHAR      charAt8     (pANTLR3_STRING string, ANTLR3_UINT32 offset);
81 static    ANTLR3_UCHAR      charAt16    (pANTLR3_STRING string, ANTLR3_UINT32 offset);
82 static    pANTLR3_STRING    subString8  (pANTLR3_STRING string, ANTLR3_UINT32 startIndex, ANTLR3_UINT32 endIndex);
83 static    pANTLR3_STRING    subString16 (pANTLR3_STRING string, ANTLR3_UINT32 startIndex, ANTLR3_UINT32 endIndex);
84 static    ANTLR3_INT32      toInt32_8   (pANTLR3_STRING string);
85 static    ANTLR3_INT32      toInt32_16  (pANTLR3_STRING string);
86 static    pANTLR3_STRING    to8_8               (pANTLR3_STRING string);
87 static    pANTLR3_STRING    to8_16              (pANTLR3_STRING string);
88 static  pANTLR3_STRING          toUTF8_8        (pANTLR3_STRING string);
89 static  pANTLR3_STRING          toUTF8_16       (pANTLR3_STRING string);
90
91 /* Local helpers
92  */
93 static  void                    stringInit8     (pANTLR3_STRING string);
94 static  void                    stringInit16    (pANTLR3_STRING string);
95 static  void    ANTLR3_CDECL    stringFree      (pANTLR3_STRING string);
96
97 ANTLR3_API pANTLR3_STRING_FACTORY 
98 antlr3StringFactoryNew()
99 {
100         pANTLR3_STRING_FACTORY  factory;
101
102         /* Allocate memory
103         */
104         factory = (pANTLR3_STRING_FACTORY) ANTLR3_MALLOC(sizeof(ANTLR3_STRING_FACTORY));
105
106         if      (factory == NULL)
107         {
108                 return  NULL;
109         }
110
111         /* Now we make a new list to track the strings.
112         */
113         factory->strings        = antlr3VectorNew(0);
114         factory->index  = 0;
115
116         if      (factory->strings == NULL)
117         {
118                 ANTLR3_FREE(factory);
119                 return  NULL;
120         }
121
122         /* Install the API (8 bit assumed)
123         */
124         factory->newRaw         =  newRaw8;
125         factory->newSize        =  newSize8;
126
127         factory->newPtr         =  newPtr8;
128         factory->newPtr8        =  newPtr8;
129         factory->newStr         =  newStr8;
130         factory->newStr8        =  newStr8;
131         factory->destroy        =  destroy;
132         factory->printable      =  printable8;
133         factory->destroy        =  destroy;
134         factory->close          =  closeFactory;
135
136         return  factory;
137 }
138
139 /** Create a string factory that is UCS2 (16 bit) encoding based
140  */
141 ANTLR3_API pANTLR3_STRING_FACTORY 
142 antlr3UCS2StringFactoryNew()
143 {
144      pANTLR3_STRING_FACTORY  factory;
145
146     /* Allocate an 8 bit factory, then override with 16 bit UCS2 functions where we 
147      * need to.
148      */
149     factory     = antlr3StringFactoryNew();
150
151     if  (factory == NULL)
152     {
153                 return  NULL;
154     }
155
156     /* Override the 8 bit API with the UCS2 (mostly just 16 bit) API
157      */
158     factory->newRaw     =  newRaw16;
159     factory->newSize    =  newSize16;
160
161     factory->newPtr     =  newPtr16_16;
162     factory->newPtr8    =  newPtr16_8;
163     factory->newStr     =  newStr16_16;
164     factory->newStr8    =  newStr16_8;
165     factory->printable  =  printable16;
166
167     factory->destroy    =  destroy;
168     factory->destroy    =  destroy;
169     factory->close      =  closeFactory;
170
171     return  factory;
172 }
173
174 /**
175  *
176  * \param factory 
177  * \return 
178  */
179 static    pANTLR3_STRING    
180 newRaw8 (pANTLR3_STRING_FACTORY factory)
181 {
182     pANTLR3_STRING  string;
183
184     string  = (pANTLR3_STRING) ANTLR3_MALLOC(sizeof(ANTLR3_STRING));
185
186     if  (string == NULL)
187     {
188                 return  NULL;
189     }
190
191     /* Structure is allocated, now fill in the API etc.
192      */
193     stringInit8(string);
194     string->factory = factory;
195
196     /* Add the string into the allocated list
197      */
198     factory->strings->set(factory->strings, factory->index, (void *) string, (void (ANTLR3_CDECL *)(void *))(stringFree), ANTLR3_TRUE);
199     string->index   = factory->index++;
200
201     return string;
202 }
203 /**
204  *
205  * \param factory 
206  * \return 
207  */
208 static    pANTLR3_STRING    
209 newRaw16        (pANTLR3_STRING_FACTORY factory)
210 {
211     pANTLR3_STRING  string;
212
213     string  = (pANTLR3_STRING) ANTLR3_MALLOC(sizeof(ANTLR3_STRING));
214
215     if  (string == NULL)
216     {
217                 return  NULL;
218     }
219
220     /* Structure is allocated, now fill in the API etc.
221      */
222     stringInit16(string);
223     string->factory = factory;
224
225     /* Add the string into the allocated list
226      */
227     factory->strings->set(factory->strings, factory->index, (void *) string, (void (ANTLR3_CDECL *)(void *))(stringFree), ANTLR3_TRUE);
228     string->index   = factory->index++;
229
230     return string;
231 }
232 static   
233 void    ANTLR3_CDECL stringFree  (pANTLR3_STRING string)
234 {
235     /* First free the string itself if there was anything in it
236      */
237     if  (string->chars)
238     {
239         ANTLR3_FREE(string->chars);
240     }
241
242     /* Now free the space for this string
243      */
244     ANTLR3_FREE(string);
245
246     return;
247 }
248 /**
249  *
250  * \param string 
251  * \return 
252  */
253 static  void
254 stringInit8  (pANTLR3_STRING string)
255 {
256     string->len                 = 0;
257     string->size                = 0;
258     string->chars               = NULL;
259     string->encoding    = ANTLR3_ENCODING_LATIN1;
260
261     /* API for 8 bit strings*/
262
263     string->set         = set8;
264     string->set8        = set8;
265     string->append      = append8;
266     string->append8     = append8;
267     string->insert      = insert8;
268     string->insert8     = insert8;
269     string->addi        = addi8;
270     string->inserti     = inserti8;
271     string->addc        = addc8;
272     string->charAt      = charAt8;
273     string->compare     = compare8;
274     string->compare8    = compare8;
275     string->subString   = subString8;
276     string->toInt32     = toInt32_8;
277     string->to8         = to8_8;
278         string->toUTF8  = toUTF8_8;
279     string->compareS    = compareS;
280     string->setS        = setS;
281     string->appendS     = appendS;
282     string->insertS     = insertS;
283
284 }
285 /**
286  *
287  * \param string 
288  * \return 
289  */
290 static  void
291 stringInit16  (pANTLR3_STRING string)
292 {
293     string->len         = 0;
294     string->size        = 0;
295     string->chars       = NULL;
296     string->encoding    = ANTLR3_ENCODING_UCS2;
297
298     /* API for 16 bit strings */
299
300     string->set         = set16_16;
301     string->set8        = set16_8;
302     string->append      = append16_16;
303     string->append8     = append16_8;
304     string->insert      = insert16_16;
305     string->insert8     = insert16_8;
306     string->addi        = addi16;
307     string->inserti     = inserti16;
308     string->addc        = addc16;
309     string->charAt      = charAt16;
310     string->compare     = compare16_16;
311     string->compare8    = compare16_8;
312     string->subString   = subString16;
313     string->toInt32     = toInt32_16;
314     string->to8         = to8_16;
315         string->toUTF8  = toUTF8_16;
316
317     string->compareS    = compareS;
318     string->setS        = setS;
319     string->appendS     = appendS;
320     string->insertS     = insertS;
321 }
322 /**
323  *
324  * \param string 
325  * \return 
326  * TODO: Implement UTF-8
327  */
328 static  void
329 stringInitUTF8  (pANTLR3_STRING string)
330 {
331     string->len     = 0;
332     string->size    = 0;
333     string->chars   = NULL;
334
335     /* API */
336
337 }
338
339 // Convert an 8 bit string into a UTF8 representation, which is in fact just the string itself
340 // a memcpy as we make no assumptions about the 8 bit encoding.
341 //
342 static  pANTLR3_STRING          
343 toUTF8_8        (pANTLR3_STRING string)
344 {
345         return string->factory->newPtr(string->factory, (pANTLR3_UINT8)(string->chars), string->len);
346 }
347
348 // Convert a 16 bit (UCS2) string into a UTF8 representation using the Unicode.org
349 // supplied C algorithms, which are now contained within the ANTLR3 C runtime
350 // as permitted by the Unicode license (within the source code antlr3convertutf.c/.h
351 // UCS2 has the same encoding as UTF16 so we can use UTF16 converter.
352 //
353 static  pANTLR3_STRING  
354 toUTF8_16       (pANTLR3_STRING string)
355 {
356         
357         UTF8                            * outputEnd;    
358         UTF16                           * inputEnd;
359         pANTLR3_STRING          utf8String;
360
361         ConversionResult        cResult;
362
363         // Allocate the output buffer, which needs to accommodate potentially
364         // 3X (in bytes) the input size (in chars).
365         //
366         utf8String      = string->factory->newStr8(string->factory, (pANTLR3_UINT8)"");
367
368         if      (utf8String != NULL)
369         {
370                 // Free existing allocation
371                 //
372                 ANTLR3_FREE(utf8String->chars);
373
374                 // Reallocate according to maximum expected size
375                 //
376                 utf8String->size        = string->len *3;
377                 utf8String->chars       = (pANTLR3_UINT8)ANTLR3_MALLOC(utf8String->size +1);
378
379                 if      (utf8String->chars != NULL)
380                 {
381                         inputEnd  = (UTF16 *)   (string->chars);
382                         outputEnd = (UTF8 *)    (utf8String->chars);
383
384                         // Call the Unicode converter
385                         //
386                         cResult =  ConvertUTF16toUTF8
387                                                         (
388                                                                 (const UTF16**)&inputEnd, 
389                                                                 ((const UTF16 *)(string->chars)) + string->len, 
390                                                                 &outputEnd, 
391                                                                 outputEnd + utf8String->size - 1,
392                                                                 lenientConversion
393                                                         );
394
395                         // We don't really care if things failed or not here, we just converted
396                         // everything that was vaguely possible and stopped when it wasn't. It is
397                         // up to the grammar programmer to verify that the input is sensible.
398                         //
399                         utf8String->len = ANTLR3_UINT32_CAST(((pANTLR3_UINT8)outputEnd) - utf8String->chars);
400
401                         *(outputEnd+1) = '\0';          // Always null terminate
402                 }
403         }
404         return utf8String;
405 }
406
407 /**
408  * Creates a new string with enough capacity for size 8 bit characters plus a terminator.
409  *
410  * \param[in] factory - Pointer to the string factory that owns strings
411  * \param[in] size - In characters
412  * \return pointer to the new string.
413  */
414 static    pANTLR3_STRING    
415 newSize8        (pANTLR3_STRING_FACTORY factory, ANTLR3_UINT32 size)
416 {
417     pANTLR3_STRING  string;
418
419     string  = factory->newRaw(factory);
420
421     if  (string == NULL)
422     {
423                 return  string;
424     }
425
426     /* Always add one more byte for a terminator ;-)
427      */
428     string->chars               = (pANTLR3_UINT8) ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_UINT8) * (size+1)));
429         *(string->chars)        = '\0';
430     string->size                = size + 1;
431
432
433     return string;
434 }
435 /**
436  * Creates a new string with enough capacity for size 16 bit characters plus a terminator.
437  *
438  * \param[in] factory - POitner to the string factory that owns strings
439  * \param[in] size - In characters
440  * \return pointer to the new string.
441  */
442 static    pANTLR3_STRING    
443 newSize16       (pANTLR3_STRING_FACTORY factory, ANTLR3_UINT32 size)
444 {
445     pANTLR3_STRING  string;
446
447     string  = factory->newRaw(factory);
448
449     if  (string == NULL)
450     {
451                 return  string;
452     }
453
454     /* Always add one more byte for a terminator ;-)
455      */ 
456     string->chars               = (pANTLR3_UINT8) ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_UINT16) * (size+1)));
457         *(string->chars)        = '\0';
458     string->size                = size+1;       /* Size is always in characters, as is len */
459
460     return string;
461 }
462
463 /** Creates a new 8 bit string initialized with the 8 bit characters at the 
464  *  supplied ptr, of pre-determined size.
465  * \param[in] factory - Pointer to the string factory that owns the strings
466  * \param[in] ptr - Pointer to 8 bit encoded characters
467  * \return pointer to the new string
468  */
469 static    pANTLR3_STRING    
470 newPtr8 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 ptr, ANTLR3_UINT32 size)
471 {
472         pANTLR3_STRING  string;
473
474         string  = factory->newSize(factory, size);
475
476         if      (string == NULL)
477         {
478                 return  NULL;
479         }
480
481         if      (size <= 0)
482         {
483                 return  string;
484         }
485
486         if      (ptr != NULL)
487         {
488                 ANTLR3_MEMMOVE(string->chars, (const void *)ptr, size);
489                 *(string->chars + size) = '\0';     /* Terminate, these strings are usually used for Token streams and printing etc.    */
490                 string->len = size;
491         }
492
493         return  string;
494 }
495
496 /** Creates a new 16 bit string initialized with the 8 bit characters at the 
497  *  supplied 8 bit character ptr, of pre-determined size.
498  * \param[in] factory - Pointer to the string factory that owns the strings
499  * \param[in] ptr - Pointer to 8 bit encoded characters
500  * \return pointer to the new string
501  */
502 static    pANTLR3_STRING    
503 newPtr16_8      (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 ptr, ANTLR3_UINT32 size)
504 {
505         pANTLR3_STRING  string;
506
507         /* newSize accepts size in characters, not bytes
508         */
509         string  = factory->newSize(factory, size);
510
511         if      (string == NULL)
512         {
513                 return  NULL;
514         }
515
516         if      (size <= 0)
517         {
518                 return  string;
519         }
520
521         if      (ptr != NULL)
522         {
523                 pANTLR3_UINT16  out;
524                 ANTLR3_INT32    inSize;
525
526                 out = (pANTLR3_UINT16)(string->chars);
527                 inSize  = size;
528
529                 while   (inSize-- > 0)
530                 {
531                         *out++ = (ANTLR3_UINT16)(*ptr++);
532                 }
533
534                 /* Terminate, these strings are usually used for Token streams and printing etc.        
535                 */
536                 *(((pANTLR3_UINT16)(string->chars)) + size) = '\0';
537
538                 string->len = size;
539         }
540
541         return  string;
542 }
543
544 /** Creates a new 16 bit string initialized with the 16 bit characters at the 
545  *  supplied ptr, of pre-determined size.
546  * \param[in] factory - Pointer to the string factory that owns the strings
547  * \param[in] ptr - Pointer to 16 bit encoded characters
548  * \return pointer to the new string
549  */
550 static    pANTLR3_STRING    
551 newPtr16_16     (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 ptr, ANTLR3_UINT32 size)
552 {
553         pANTLR3_STRING  string;
554
555         string  = factory->newSize(factory, size);
556
557         if      (string == NULL)
558         {
559                 return  NULL;
560         }
561
562         if      (size <= 0)
563         {
564                 return  string;
565         }
566
567         if      (ptr != NULL)
568         {
569                 ANTLR3_MEMMOVE(string->chars, (const void *)ptr, (size * sizeof(ANTLR3_UINT16)));
570
571                 /* Terminate, these strings are usually used for Token streams and printing etc.        
572                 */
573                 *(((pANTLR3_UINT16)(string->chars)) + size) = '\0';         
574                 string->len = size;
575         }
576
577         return  string;
578 }
579
580 /** Create a new 8 bit string from the supplied, null terminated, 8 bit string pointer.
581  * \param[in] factory - Pointer to the string factory that owns strings.
582  * \param[in] ptr - Pointer to the 8 bit encoded string
583  * \return Pointer to the newly initialized string
584  */
585 static    pANTLR3_STRING    
586 newStr8 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 ptr)
587 {
588     return factory->newPtr8(factory, ptr, (ANTLR3_UINT32)strlen((const char *)ptr));
589 }
590
591 /** Create a new 16 bit string from the supplied, null terminated, 8 bit string pointer.
592  * \param[in] factory - Pointer to the string factory that owns strings.
593  * \param[in] ptr - Pointer to the 8 bit encoded string
594  * \return POinter to the newly initialized string
595  */
596 static    pANTLR3_STRING    
597 newStr16_8      (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 ptr)
598 {
599     return factory->newPtr8(factory, ptr, (ANTLR3_UINT32)strlen((const char *)ptr));
600 }
601
602 /** Create a new 16 bit string from the supplied, null terminated, 16 bit string pointer.
603  * \param[in] factory - Pointer to the string factory that owns strings.
604  * \param[in] ptr - Pointer to the 16 bit encoded string
605  * \return Pointer to the newly initialized string
606  */
607 static    pANTLR3_STRING    
608 newStr16_16     (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 ptr)
609 {
610     pANTLR3_UINT16  in;
611     ANTLR3_UINT32   count;
612
613     /** First, determine the length of the input string
614      */
615     in      = (pANTLR3_UINT16)ptr;
616     count   = 0;
617
618     while   (*in++ != '\0')
619     {
620                 count++;
621     }
622     return factory->newPtr(factory, ptr, count);
623 }
624
625 static    void              
626 destroy (pANTLR3_STRING_FACTORY factory, pANTLR3_STRING string)
627 {
628     // Record which string we are deleting
629     //
630     ANTLR3_UINT32 strIndex = string->index;
631     
632     // Ensure that the string was not factory made, or we would try
633     // to delete memory that wasn't allocated outside the factory
634     // block.
635     // Remove the specific indexed string from the vector
636     //
637     factory->strings->del(factory->strings, strIndex);
638
639     // One less string in the vector, so decrement the factory index
640     // so that the next string allocated is indexed correctly with
641     // respect to the vector.
642     //
643     factory->index--;
644
645     // Now we have to reindex the strings in the vector that followed
646     // the one we just deleted. We only do this if the one we just deleted
647     // was not the last one.
648     //
649     if  (strIndex< factory->index)
650     {
651         // We must reindex the strings after the one we just deleted.
652         // The one that follows the one we just deleted is also out
653         // of whack, so we start there.
654         //
655         ANTLR3_UINT32 i;
656
657         for (i = strIndex; i < factory->index; i++)
658         {
659             // Renumber the entry
660             //
661             ((pANTLR3_STRING)(factory->strings->elements[i].element))->index = i;
662         }
663     }
664
665     // The string has been destroyed and the elements of the factory are reindexed.
666     //
667
668 }
669
670 static    pANTLR3_STRING    
671 printable8(pANTLR3_STRING_FACTORY factory, pANTLR3_STRING instr)
672 {
673     pANTLR3_STRING  string;
674     
675     /* We don't need to be too efficient here, this is mostly for error messages and so on.
676      */
677     pANTLR3_UINT8   scannedText;
678     ANTLR3_UINT32   i;
679
680     /* Assume we need as much as twice as much space to parse out the control characters
681      */
682     string  = factory->newSize(factory, instr->len *2 + 1);
683
684     /* Scan through and replace unprintable (in terms of this routine)
685      * characters
686      */
687     scannedText = string->chars;
688
689     for (i = 0; i < instr->len; i++)
690     {
691                 if (*(instr->chars + i) == '\n')
692                 {
693                         *scannedText++ = '\\';
694                         *scannedText++ = 'n';
695                 }
696                 else if (*(instr->chars + i) == '\r')
697                 {
698                         *scannedText++ = '\\';
699                         *scannedText++ = 'r';
700                 }
701                 else if (!isprint(*(instr->chars +i)))
702                 {
703                         *scannedText++ = '?';
704                 }
705                 else
706                 {
707                         *scannedText++ = *(instr->chars + i);
708                 }
709     }
710     *scannedText  = '\0';
711
712     string->len = (ANTLR3_UINT32)(scannedText - string->chars);
713     
714     return  string;
715 }
716
717 static    pANTLR3_STRING    
718 printable16(pANTLR3_STRING_FACTORY factory, pANTLR3_STRING instr)
719 {
720     pANTLR3_STRING  string;
721     
722     /* We don't need to be too efficient here, this is mostly for error messages and so on.
723      */
724     pANTLR3_UINT16  scannedText;
725     pANTLR3_UINT16  inText;
726     ANTLR3_UINT32   i;
727     ANTLR3_UINT32   outLen;
728
729     /* Assume we need as much as twice as much space to parse out the control characters
730      */
731     string  = factory->newSize(factory, instr->len *2 + 1);
732
733     /* Scan through and replace unprintable (in terms of this routine)
734      * characters
735      */
736     scannedText = (pANTLR3_UINT16)(string->chars);
737     inText      = (pANTLR3_UINT16)(instr->chars);
738     outLen      = 0;
739
740     for (i = 0; i < instr->len; i++)
741     {
742                 if (*(inText + i) == '\n')
743                 {
744                         *scannedText++   = '\\';
745                         *scannedText++   = 'n';
746                         outLen      += 2;
747                 }
748                 else if (*(inText + i) == '\r')
749                 {
750                         *scannedText++   = '\\';
751                         *scannedText++   = 'r';
752                         outLen      += 2;
753                 }
754                 else if (!isprint(*(inText +i)))
755                 {
756                         *scannedText++ = '?';
757                         outLen++;
758                 }
759                 else
760                 {
761                         *scannedText++ = *(inText + i);
762                         outLen++;
763                 }
764     }
765     *scannedText  = '\0';
766
767     string->len = outLen;
768     
769     return  string;
770 }
771
772 /** Fascist Capitalist Pig function created
773  *  to oppress the workers comrade.
774  */
775 static    void              
776 closeFactory    (pANTLR3_STRING_FACTORY factory)
777 {
778     /* Delete the vector we were tracking the strings with, this will
779      * causes all the allocated strings to be deallocated too
780      */
781     factory->strings->free(factory->strings);
782
783     /* Delete the space for the factory itself
784      */
785     ANTLR3_FREE((void *)factory);
786 }
787
788 static    pANTLR3_UINT8   
789 append8 (pANTLR3_STRING string, const char * newbit)
790 {
791     ANTLR3_UINT32 len;
792
793     len = (ANTLR3_UINT32)strlen(newbit);
794
795     if  (string->size < (string->len + len + 1))
796     {
797                 string->chars   = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(string->len + len + 1));
798                 string->size    = string->len + len + 1;
799     }
800
801     /* Note we copy one more byte than the strlen in order to get the trailing
802      */
803     ANTLR3_MEMMOVE((void *)(string->chars + string->len), newbit, (ANTLR3_UINT32)(len+1));
804     string->len += len;
805
806     return string->chars;
807 }
808
809 static    pANTLR3_UINT8   
810 append16_8      (pANTLR3_STRING string, const char * newbit)
811 {
812     ANTLR3_UINT32   len;
813     pANTLR3_UINT16  apPoint;
814     ANTLR3_UINT32   count;
815
816     len = (ANTLR3_UINT32)strlen(newbit);
817
818     if  (string->size < (string->len + len + 1))
819     {
820                 string->chars   = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)((sizeof(ANTLR3_UINT16)*(string->len + len + 1))));
821                 string->size    = string->len + len + 1;
822     }
823
824     apPoint = ((pANTLR3_UINT16)string->chars) + string->len;
825     string->len += len;
826
827     for (count = 0; count < len; count++)
828     {
829                 *apPoint++   = *(newbit + count);
830     }
831     *apPoint = '\0';
832
833     return string->chars;
834 }
835
836 static    pANTLR3_UINT8   
837 append16_16     (pANTLR3_STRING string, const char * newbit)
838 {
839     ANTLR3_UINT32 len;
840     pANTLR3_UINT16  in;
841
842     /** First, determine the length of the input string
843      */
844     in      = (pANTLR3_UINT16)newbit;
845     len   = 0;
846
847     while   (*in++ != '\0')
848     {
849                 len++;
850     }
851
852     if  (string->size < (string->len + len + 1))
853     {
854                 string->chars   = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)( sizeof(ANTLR3_UINT16) *(string->len + len + 1) ));
855                 string->size    = string->len + len + 1;
856     }
857
858     /* Note we copy one more byte than the strlen in order to get the trailing delimiter
859      */
860     ANTLR3_MEMMOVE((void *)(((pANTLR3_UINT16)string->chars) + string->len), newbit, (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16)*(len+1)));
861     string->len += len;
862
863     return string->chars;
864 }
865
866 static    pANTLR3_UINT8   
867 set8    (pANTLR3_STRING string, const char * chars)
868 {
869     ANTLR3_UINT32       len;
870
871     len = (ANTLR3_UINT32)strlen(chars);
872     if  (string->size < len + 1)
873     {
874                 string->chars   = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(len + 1));
875                 string->size    = len + 1;
876     }
877
878     /* Note we copy one more byte than the strlen in order to get the trailing '\0'
879      */
880     ANTLR3_MEMMOVE((void *)(string->chars), chars, (ANTLR3_UINT32)(len+1));
881     string->len     = len;
882
883     return  string->chars;
884
885 }
886
887 static    pANTLR3_UINT8   
888 set16_8 (pANTLR3_STRING string, const char * chars)
889 {
890     ANTLR3_UINT32       len;
891     ANTLR3_UINT32       count;
892     pANTLR3_UINT16      apPoint;
893
894     len = (ANTLR3_UINT32)strlen(chars);
895     if  (string->size < len + 1)
896         {
897                 string->chars   = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16)*(len + 1)));
898                 string->size    = len + 1;
899     }
900     apPoint = ((pANTLR3_UINT16)string->chars);
901     string->len = len;
902
903     for (count = 0; count < string->len; count++)
904     {
905                 *apPoint++   = *(chars + count);
906     }
907     *apPoint = '\0';
908
909     return  string->chars;
910 }
911
912 static    pANTLR3_UINT8   
913 set16_16    (pANTLR3_STRING string, const char * chars)
914 {
915     ANTLR3_UINT32   len;
916     pANTLR3_UINT16  in;
917
918     /** First, determine the length of the input string
919      */
920     in      = (pANTLR3_UINT16)chars;
921     len   = 0;
922
923     while   (*in++ != '\0')
924     {
925                 len++;
926     }
927
928     if  (string->size < len + 1)
929     {
930                 string->chars   = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16)*(len + 1)));
931                 string->size    = len + 1;
932     }
933
934     /* Note we copy one more byte than the strlen in order to get the trailing '\0'
935      */
936     ANTLR3_MEMMOVE((void *)(string->chars), chars, (ANTLR3_UINT32)((len+1) * sizeof(ANTLR3_UINT16)));
937     string->len     = len;
938
939     return  string->chars;
940
941 }
942
943 static    pANTLR3_UINT8   
944 addc8   (pANTLR3_STRING string, ANTLR3_UINT32 c)
945 {
946     if  (string->size < string->len + 2)
947     {
948                 string->chars   = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(string->len + 2));
949                 string->size    = string->len + 2;
950     }
951     *(string->chars + string->len)      = (ANTLR3_UINT8)c;
952     *(string->chars + string->len + 1)  = '\0';
953     string->len++;
954
955     return  string->chars;
956 }
957
958 static    pANTLR3_UINT8   
959 addc16  (pANTLR3_STRING string, ANTLR3_UINT32 c)
960 {
961     pANTLR3_UINT16  ptr;
962
963     if  (string->size < string->len + 2)
964     {
965                 string->chars   = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16) * (string->len + 2)));
966                 string->size    = string->len + 2;
967     }
968     ptr = (pANTLR3_UINT16)(string->chars);
969
970     *(ptr + string->len)        = (ANTLR3_UINT16)c;
971     *(ptr + string->len + 1)    = '\0';
972     string->len++;
973
974     return  string->chars;
975 }
976
977 static    pANTLR3_UINT8   
978 addi8   (pANTLR3_STRING string, ANTLR3_INT32 i)
979 {
980     ANTLR3_UINT8            newbit[32];
981
982     sprintf((char *)newbit, "%d", i);
983
984     return  string->append8(string, (const char *)newbit);
985 }
986 static    pANTLR3_UINT8   
987 addi16  (pANTLR3_STRING string, ANTLR3_INT32 i)
988 {
989     ANTLR3_UINT8            newbit[32];
990
991     sprintf((char *)newbit, "%d", i);
992
993     return  string->append8(string, (const char *)newbit);
994 }
995
996 static    pANTLR3_UINT8
997 inserti8    (pANTLR3_STRING string, ANTLR3_UINT32 point, ANTLR3_INT32 i)
998 {
999     ANTLR3_UINT8            newbit[32];
1000
1001     sprintf((char *)newbit, "%d", i);
1002     return  string->insert8(string, point, (const char *)newbit);
1003 }
1004 static    pANTLR3_UINT8
1005 inserti16    (pANTLR3_STRING string, ANTLR3_UINT32 point, ANTLR3_INT32 i)
1006 {
1007     ANTLR3_UINT8            newbit[32];
1008
1009     sprintf((char *)newbit, "%d", i);
1010     return  string->insert8(string, point, (const char *)newbit);
1011 }
1012
1013 static  pANTLR3_UINT8
1014 insert8 (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit)
1015 {
1016     ANTLR3_UINT32       len;
1017
1018     if  (point >= string->len)
1019     {
1020                 return  string->append(string, newbit);
1021     }
1022  
1023     len = (ANTLR3_UINT32)strlen(newbit);
1024
1025     if  (len == 0)
1026     {
1027                 return  string->chars;
1028     }
1029
1030     if  (string->size < (string->len + len + 1))
1031     {
1032                 string->chars   = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(string->len + len + 1));
1033                 string->size    = string->len + len + 1;
1034     }
1035
1036     /* Move the characters we are inserting before, including the delimiter
1037      */
1038     ANTLR3_MEMMOVE((void *)(string->chars + point + len), (void *)(string->chars + point), (ANTLR3_UINT32)(string->len - point + 1));
1039
1040     /* Note we copy the exact number of bytes
1041      */
1042     ANTLR3_MEMMOVE((void *)(string->chars + point), newbit, (ANTLR3_UINT32)(len));
1043     
1044     string->len += len;
1045
1046     return  string->chars;
1047 }
1048
1049 static  pANTLR3_UINT8
1050 insert16_8      (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit)
1051 {
1052     ANTLR3_UINT32       len;
1053     ANTLR3_UINT32       count;
1054     pANTLR3_UINT16      inPoint;
1055
1056     if  (point >= string->len)
1057     {
1058                 return  string->append8(string, newbit);
1059     }
1060  
1061     len = (ANTLR3_UINT32)strlen(newbit);
1062
1063     if  (len == 0)
1064     {
1065                 return  string->chars;
1066     }
1067
1068     if  (string->size < (string->len + len + 1))
1069     {
1070         string->chars   = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16)*(string->len + len + 1)));
1071         string->size    = string->len + len + 1;
1072     }
1073
1074     /* Move the characters we are inserting before, including the delimiter
1075      */
1076     ANTLR3_MEMMOVE((void *)(((pANTLR3_UINT16)string->chars) + point + len), (void *)(((pANTLR3_UINT16)string->chars) + point), (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16)*(string->len - point + 1)));
1077
1078     string->len += len;
1079     
1080     inPoint = ((pANTLR3_UINT16)(string->chars))+point;
1081     for (count = 0; count<len; count++)
1082     {
1083                 *(inPoint + count) = (ANTLR3_UINT16)(*(newbit+count));
1084     }
1085
1086     return  string->chars;
1087 }
1088
1089 static  pANTLR3_UINT8
1090 insert16_16     (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit)
1091 {
1092     ANTLR3_UINT32       len;
1093     pANTLR3_UINT16      in;
1094
1095     if  (point >= string->len)
1096     {
1097                 return  string->append(string, newbit);
1098     }
1099  
1100     /** First, determine the length of the input string
1101      */
1102     in      = (pANTLR3_UINT16)newbit;
1103     len     = 0;
1104
1105     while   (*in++ != '\0')
1106     {
1107                 len++;
1108     }
1109
1110     if  (len == 0)
1111     {
1112                 return  string->chars;
1113     }
1114
1115     if  (string->size < (string->len + len + 1))
1116     {
1117                 string->chars   = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16)*(string->len + len + 1)));
1118                 string->size    = string->len + len + 1;
1119     }
1120
1121     /* Move the characters we are inserting before, including the delimiter
1122      */
1123     ANTLR3_MEMMOVE((void *)(((pANTLR3_UINT16)string->chars) + point + len), (void *)(((pANTLR3_UINT16)string->chars) + point), (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16)*(string->len - point + 1)));
1124
1125
1126     /* Note we copy the exact number of characters
1127      */
1128     ANTLR3_MEMMOVE((void *)(((pANTLR3_UINT16)string->chars) + point), newbit, (ANTLR3_UINT32)(sizeof(ANTLR3_UINT16)*(len)));
1129     
1130     string->len += len;
1131
1132     return  string->chars;
1133 }
1134
1135 static    pANTLR3_UINT8     setS        (pANTLR3_STRING string, pANTLR3_STRING chars)
1136 {
1137     return  string->set(string, (const char *)(chars->chars));
1138 }
1139
1140 static    pANTLR3_UINT8     appendS     (pANTLR3_STRING string, pANTLR3_STRING newbit)
1141 {
1142     /* We may be passed an empty string, in which case we just return the current pointer
1143      */
1144     if  (newbit == NULL || newbit->len == 0 || newbit->size == 0 || newbit->chars == NULL)
1145     {
1146                 return  string->chars;
1147     }
1148     else
1149     {
1150                 return  string->append(string, (const char *)(newbit->chars));
1151     }
1152 }
1153
1154 static    pANTLR3_UINT8     insertS     (pANTLR3_STRING string, ANTLR3_UINT32 point, pANTLR3_STRING newbit)
1155 {
1156     return  string->insert(string, point, (const char *)(newbit->chars));
1157 }
1158
1159 /* Function that compares the text of a string to the supplied
1160  * 8 bit character string and returns a result a la strcmp()
1161  */
1162 static ANTLR3_UINT32   
1163 compare8        (pANTLR3_STRING string, const char * compStr)
1164 {
1165     return  strcmp((const char *)(string->chars), compStr);
1166 }
1167
1168 /* Function that compares the text of a string with the supplied character string
1169  * (which is assumed to be in the same encoding as the string itself) and returns a result
1170  * a la strcmp()
1171  */
1172 static ANTLR3_UINT32   
1173 compare16_8     (pANTLR3_STRING string, const char * compStr)
1174 {
1175     pANTLR3_UINT16  ourString;
1176     ANTLR3_UINT32   charDiff;
1177
1178     ourString   = (pANTLR3_UINT16)(string->chars);
1179
1180     while   (((ANTLR3_UCHAR)(*ourString) != '\0') && ((ANTLR3_UCHAR)(*compStr) != '\0'))
1181     {
1182                 charDiff = *ourString - *compStr;
1183                 if  (charDiff != 0)
1184                 {
1185                         return charDiff;
1186                 }
1187                 ourString++;
1188                 compStr++;
1189     }
1190
1191     /* At this point, one of the strings was terminated
1192      */
1193     return (ANTLR3_UINT32)((ANTLR3_UCHAR)(*ourString) - (ANTLR3_UCHAR)(*compStr));
1194
1195 }
1196
1197 /* Function that compares the text of a string with the supplied character string
1198  * (which is assumed to be in the same encoding as the string itself) and returns a result
1199  * a la strcmp()
1200  */
1201 static ANTLR3_UINT32   
1202 compare16_16    (pANTLR3_STRING string, const char * compStr8)
1203 {
1204     pANTLR3_UINT16  ourString;
1205     pANTLR3_UINT16  compStr;
1206     ANTLR3_UINT32   charDiff;
1207
1208     ourString   = (pANTLR3_UINT16)(string->chars);
1209     compStr     = (pANTLR3_UINT16)(compStr8);
1210
1211     while   (((ANTLR3_UCHAR)(*ourString) != '\0') && ((ANTLR3_UCHAR)(*((pANTLR3_UINT16)compStr)) != '\0'))
1212     {
1213                 charDiff = *ourString - *compStr;
1214                 if  (charDiff != 0)
1215                 {
1216                         return charDiff;
1217                 }
1218                 ourString++;
1219                 compStr++;
1220     }
1221
1222     /* At this point, one of the strings was terminated
1223      */
1224     return (ANTLR3_UINT32)((ANTLR3_UCHAR)(*ourString) - (ANTLR3_UCHAR)(*compStr));
1225 }
1226
1227 /* Function that compares the text of a string with the supplied string
1228  * (which is assumed to be in the same encoding as the string itself) and returns a result
1229  * a la strcmp()
1230  */
1231 static ANTLR3_UINT32   
1232 compareS    (pANTLR3_STRING string, pANTLR3_STRING compStr)
1233 {
1234     return  string->compare(string, (const char *)compStr->chars);
1235 }
1236
1237
1238 /* Function that returns the character indexed at the supplied
1239  * offset as a 32 bit character.
1240  */
1241 static ANTLR3_UCHAR    
1242 charAt8     (pANTLR3_STRING string, ANTLR3_UINT32 offset)
1243 {
1244     if  (offset > string->len)
1245     {
1246                 return (ANTLR3_UCHAR)'\0';
1247     }
1248     else
1249     {
1250                 return  (ANTLR3_UCHAR)(*(string->chars + offset));
1251     }
1252 }
1253
1254 /* Function that returns the character indexed at the supplied
1255  * offset as a 32 bit character.
1256  */
1257 static ANTLR3_UCHAR    
1258 charAt16    (pANTLR3_STRING string, ANTLR3_UINT32 offset)
1259 {
1260     if  (offset > string->len)
1261     {
1262                 return (ANTLR3_UCHAR)'\0';
1263     }
1264     else
1265     {
1266                 return  (ANTLR3_UCHAR)(*((pANTLR3_UINT16)(string->chars) + offset));
1267     }
1268 }
1269
1270 /* Function that returns a substring of the supplied string a la .subString(s,e)
1271  * in java runtimes.
1272  */
1273 static pANTLR3_STRING
1274 subString8   (pANTLR3_STRING string, ANTLR3_UINT32 startIndex, ANTLR3_UINT32 endIndex)
1275 {
1276     pANTLR3_STRING newStr;
1277
1278     if  (endIndex > string->len)
1279     {
1280                 endIndex = string->len + 1;
1281     }
1282     newStr  = string->factory->newPtr(string->factory, string->chars + startIndex, endIndex - startIndex);
1283
1284     return newStr;
1285 }
1286
1287 /* Returns a substring of the supplied string a la .subString(s,e)
1288  * in java runtimes.
1289  */
1290 static pANTLR3_STRING
1291 subString16  (pANTLR3_STRING string, ANTLR3_UINT32 startIndex, ANTLR3_UINT32 endIndex)
1292 {
1293     pANTLR3_STRING newStr;
1294
1295     if  (endIndex > string->len)
1296     {
1297                 endIndex = string->len + 1;
1298     }
1299     newStr  = string->factory->newPtr(string->factory, (pANTLR3_UINT8)((pANTLR3_UINT16)(string->chars) + startIndex), endIndex - startIndex);
1300
1301     return newStr;
1302 }
1303
1304 /* Function that can convert the characters in the string to an integer
1305  */
1306 static ANTLR3_INT32
1307 toInt32_8           (struct ANTLR3_STRING_struct * string)
1308 {
1309     return  atoi((const char *)(string->chars));
1310 }
1311
1312 /* Function that can convert the characters in the string to an integer
1313  */
1314 static ANTLR3_INT32
1315 toInt32_16       (struct ANTLR3_STRING_struct * string)
1316 {
1317     pANTLR3_UINT16  input;
1318     ANTLR3_INT32   value;
1319     ANTLR3_BOOLEAN  negate;
1320
1321     value   = 0;
1322     input   = (pANTLR3_UINT16)(string->chars);
1323     negate  = ANTLR3_FALSE;
1324
1325     if  (*input == (ANTLR3_UCHAR)'-')
1326     {
1327                 negate = ANTLR3_TRUE;
1328                 input++;
1329     }
1330     else if (*input == (ANTLR3_UCHAR)'+')
1331     {
1332                 input++;
1333     }
1334
1335     while   (*input != '\0' && isdigit(*input))
1336     {
1337                 value    = value * 10;
1338                 value   += ((ANTLR3_UINT32)(*input) - (ANTLR3_UINT32)'0');
1339                 input++;
1340     }
1341
1342     return negate ? -value : value;
1343 }
1344
1345 /* Function that returns a pointer to an 8 bit version of the string,
1346  * which in this case is just the string as this is 
1347  * 8 bit encodiing anyway.
1348  */
1349 static    pANTLR3_STRING            to8_8       (pANTLR3_STRING string)
1350 {
1351     return  string;
1352 }
1353
1354 /* Function that returns an 8 bit version of the string,
1355  * which in this case is returning all the 16 bit characters
1356  * narrowed back into 8 bits, with characters that are too large
1357  * replaced with '_'
1358  */
1359 static    pANTLR3_STRING    to8_16      (pANTLR3_STRING string)
1360 {
1361         pANTLR3_STRING  newStr;
1362         ANTLR3_UINT32   i;
1363
1364         /* Create a new 8 bit string
1365         */
1366         newStr  = newRaw8(string->factory);
1367
1368         if      (newStr == NULL)
1369         {
1370                 return  NULL;
1371         }
1372
1373         /* Always add one more byte for a terminator
1374         */
1375         newStr->chars   = (pANTLR3_UINT8) ANTLR3_MALLOC((size_t)(string->len + 1));
1376         newStr->size    = string->len + 1;
1377         newStr->len         = string->len;
1378
1379         /* Now copy each 16 bit charActer , making it an 8 bit character of 
1380         * some sort.
1381         */
1382         for     (i=0; i<string->len; i++)
1383         {
1384                 ANTLR3_UCHAR    c;
1385
1386                 c = *(((pANTLR3_UINT16)(string->chars)) + i);
1387
1388                 *(newStr->chars + i) = (ANTLR3_UINT8)(c > 255 ? '_' : c);
1389         }
1390
1391         /* Terminate
1392         */
1393         *(newStr->chars + newStr->len) = '\0';
1394
1395         return newStr;
1396 }