📄 antlr3string.c
字号:
/** \file * Implementation of the ANTLR3 string and string factory classes */#include <antlr3string.h>/* Factory API */static pANTLR3_STRING newRaw8 (pANTLR3_STRING_FACTORY factory);static pANTLR3_STRING newRaw16 (pANTLR3_STRING_FACTORY factory);static pANTLR3_STRING newSize8 (pANTLR3_STRING_FACTORY factory, ANTLR3_UINT32 size);static pANTLR3_STRING newSize16 (pANTLR3_STRING_FACTORY factory, ANTLR3_UINT32 size);static pANTLR3_STRING newPtr8 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string, ANTLR3_UINT32 size);static pANTLR3_STRING newPtr16_8 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string, ANTLR3_UINT32 size);static pANTLR3_STRING newPtr16_16 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string, ANTLR3_UINT32 size);static pANTLR3_STRING newStr8 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string);static pANTLR3_STRING newStr16_8 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string);static pANTLR3_STRING newStr16_16 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 string);static void destroy (pANTLR3_STRING_FACTORY factory, pANTLR3_STRING string);static pANTLR3_STRING printable8 (pANTLR3_STRING_FACTORY factory, pANTLR3_STRING string);static pANTLR3_STRING printable16 (pANTLR3_STRING_FACTORY factory, pANTLR3_STRING string);static void closeFactory(pANTLR3_STRING_FACTORY factory);/* String API */static pANTLR3_UINT8 set8 (pANTLR3_STRING string, const char * chars);static pANTLR3_UINT8 set16_8 (pANTLR3_STRING string, const char * chars);static pANTLR3_UINT8 set16_16 (pANTLR3_STRING string, const char * chars);static pANTLR3_UINT8 append8 (pANTLR3_STRING string, const char * newbit);static pANTLR3_UINT8 append16_8 (pANTLR3_STRING string, const char * newbit);static pANTLR3_UINT8 append16_16 (pANTLR3_STRING string, const char * newbit);static pANTLR3_UINT8 insert8 (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit);static pANTLR3_UINT8 insert16_8 (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit);static pANTLR3_UINT8 insert16_16 (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit);static pANTLR3_UINT8 setS (pANTLR3_STRING string, pANTLR3_STRING chars);static pANTLR3_UINT8 appendS (pANTLR3_STRING string, pANTLR3_STRING newbit);static pANTLR3_UINT8 insertS (pANTLR3_STRING string, ANTLR3_UINT32 point, pANTLR3_STRING newbit);static pANTLR3_UINT8 addc8 (pANTLR3_STRING string, ANTLR3_UINT32 c);static pANTLR3_UINT8 addc16 (pANTLR3_STRING string, ANTLR3_UINT32 c);static pANTLR3_UINT8 addi8 (pANTLR3_STRING string, ANTLR3_INT32 i);static pANTLR3_UINT8 addi16 (pANTLR3_STRING string, ANTLR3_INT32 i);static pANTLR3_UINT8 inserti8 (pANTLR3_STRING string, ANTLR3_UINT32 point, ANTLR3_INT32 i);static pANTLR3_UINT8 inserti16 (pANTLR3_STRING string, ANTLR3_UINT32 point, ANTLR3_INT32 i);static ANTLR3_UINT32 compare8 (pANTLR3_STRING string, const char * compStr);static ANTLR3_UINT32 compare16_8 (pANTLR3_STRING string, const char * compStr);static ANTLR3_UINT32 compare16_16(pANTLR3_STRING string, const char * compStr);static ANTLR3_UINT32 compareS (pANTLR3_STRING string, pANTLR3_STRING compStr);static ANTLR3_UCHAR charAt8 (pANTLR3_STRING string, ANTLR3_UINT32 offset);static ANTLR3_UCHAR charAt16 (pANTLR3_STRING string, ANTLR3_UINT32 offset);static pANTLR3_STRING subString8 (pANTLR3_STRING string, ANTLR3_UINT32 startIndex, ANTLR3_UINT32 endIndex);static pANTLR3_STRING subString16 (pANTLR3_STRING string, ANTLR3_UINT32 startIndex, ANTLR3_UINT32 endIndex);static ANTLR3_INT32 toInt32_8 (pANTLR3_STRING string);static ANTLR3_INT32 toInt32_16 (pANTLR3_STRING string);static pANTLR3_STRING to8_8 (pANTLR3_STRING string);static pANTLR3_STRING to8_16 (pANTLR3_STRING string);/* Local helpers */static void stringInit8 (pANTLR3_STRING string);static void stringInit16 (pANTLR3_STRING string);static void ANTLR3_CDECL stringFree (pANTLR3_STRING string);ANTLR3_API pANTLR3_STRING_FACTORY antlr3StringFactoryNew(){ pANTLR3_STRING_FACTORY factory; /* Allocate memory */ factory = (pANTLR3_STRING_FACTORY) ANTLR3_MALLOC(sizeof(ANTLR3_STRING_FACTORY)); if (factory == NULL) { return (pANTLR3_STRING_FACTORY)(ANTLR3_ERR_NOMEM); } /* Now we make a new list to track the strings. */ factory->strings = antlr3VectorNew(0); factory->index = 1; if (factory->strings == (pANTLR3_VECTOR)(ANTLR3_ERR_NOMEM)) { ANTLR3_FREE(factory); return (pANTLR3_STRING_FACTORY)(ANTLR3_ERR_NOMEM); } /* Install the API (8 bit assumed) */ factory->newRaw = newRaw8; factory->newSize = newSize8; factory->newPtr = newPtr8; factory->newPtr8 = newPtr8; factory->newStr = newStr8; factory->newStr8 = newStr8; factory->destroy = destroy; factory->printable = printable8; factory->destroy = destroy; factory->close = closeFactory; return factory;}/** Create a string factory that is UCS2 (16 bit) encodingn based */ANTLR3_API pANTLR3_STRING_FACTORY antlr3UCS2StringFactoryNew(){ pANTLR3_STRING_FACTORY factory; /* Allocate an 8 bit factory, then override with 16 bit UCS2 functions where we * need to. */ factory = antlr3StringFactoryNew(); if (factory == NULL) { return (pANTLR3_STRING_FACTORY)(ANTLR3_ERR_NOMEM); } /* Override the 8 bit API with the UCS2 (mostly just 16 bit) API */ factory->newRaw = newRaw16; factory->newSize = newSize16; factory->newPtr = newPtr16_16; factory->newPtr8 = newPtr16_8; factory->newStr = newStr16_16; factory->newStr8 = newStr16_8; factory->printable = printable16; factory->destroy = destroy; factory->destroy = destroy; factory->close = closeFactory; return factory;}/** * * \param factory * \return */static pANTLR3_STRING newRaw8 (pANTLR3_STRING_FACTORY factory){ pANTLR3_STRING string; string = (pANTLR3_STRING) ANTLR3_MALLOC(sizeof(ANTLR3_STRING)); if (string == NULL) { return (pANTLR3_STRING)(ANTLR3_ERR_NOMEM); } /* Structure is allocated, now fill in the API etc. */ stringInit8(string); string->factory = factory; /* Add the string into the allocated list */ factory->strings->put(factory->strings, factory->index, (void *) string, (void (ANTLR3_CDECL *)(void *))(stringFree), ANTLR3_TRUE); string->index = factory->index++; return string;}/** * * \param factory * \return */static pANTLR3_STRING newRaw16 (pANTLR3_STRING_FACTORY factory){ pANTLR3_STRING string; string = (pANTLR3_STRING) ANTLR3_MALLOC(sizeof(ANTLR3_STRING)); if (string == NULL) { return (pANTLR3_STRING)(ANTLR3_ERR_NOMEM); } /* Structure is allocated, now fill in the API etc. */ stringInit16(string); string->factory = factory; /* Add the string into the allocated list */ factory->strings->put(factory->strings, factory->index, (void *) string, (void (ANTLR3_CDECL *)(void *))(stringFree), ANTLR3_TRUE); string->index = factory->index++; return string;}static void ANTLR3_CDECL stringFree (pANTLR3_STRING string){ /* First free the string itself if there was anything in it */ if (string->chars) { ANTLR3_FREE(string->chars); } /* Now free the space for this string */ ANTLR3_FREE(string); return;}/** * * \param string * \return */static voidstringInit8 (pANTLR3_STRING string){ string->len = 0; string->size = 0; string->chars = NULL; string->encoding = ANTLR3_ENCODING_LATIN1; /* API for 8 bit strings*/ string->set = set8; string->set8 = set8; string->append = append8; string->append8 = append8; string->insert = insert8; string->insert8 = insert8; string->addi = addi8; string->inserti = inserti8; string->addc = addc8; string->charAt = charAt8; string->compare = compare8; string->compare8 = compare8; string->subString = subString8; string->toInt32 = toInt32_8; string->to8 = to8_8; string->compareS = compareS; string->setS = setS; string->appendS = appendS; string->insertS = insertS;}/** * * \param string * \return */static voidstringInit16 (pANTLR3_STRING string){ string->len = 0; string->size = 0; string->chars = NULL; string->encoding = ANTLR3_ENCODING_UCS2; /* API for 16 bit strings */ string->set = set16_16; string->set8 = set16_8; string->append = append16_16; string->append8 = append16_8; string->insert = insert16_16; string->insert8 = insert16_8; string->addi = addi16; string->inserti = inserti16; string->addc = addc16; string->charAt = charAt16; string->compare = compare16_16; string->compare8 = compare16_8; string->subString = subString16; string->toInt32 = toInt32_16; string->to8 = to8_16; string->compareS = compareS; string->setS = setS; string->appendS = appendS; string->insertS = insertS;}/** * * \param string * \return * TODO: Implement UTF-8 */static voidstringInitUTF8 (pANTLR3_STRING string){ string->len = 0; string->size = 0; string->chars = NULL; /* API */}/** * Creates a new string with enough capacity for size 8 bit characters plus a terminator. * * \param[in] factory - Poitner to the string factory that owns strings * \param[in] size - In characters * \return pointer to the new string. */static pANTLR3_STRING newSize8 (pANTLR3_STRING_FACTORY factory, ANTLR3_UINT32 size){ pANTLR3_STRING string; string = factory->newRaw(factory); if (string == (pANTLR3_STRING)(ANTLR3_ERR_NOMEM)) { return string; } /* Always add one more byte for a terminator ;-) */ string->chars = (pANTLR3_UINT8) ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_UINT8) * (size+1))); string->size = size + 1; return string;}/** * Creates a new string with enough capacity for size 16 bit characters plus a terminator. * * \param[in] factory - POitner to the string factory that owns strings * \param[in] size - In characters * \return pointer to the new string. */static pANTLR3_STRING newSize16 (pANTLR3_STRING_FACTORY factory, ANTLR3_UINT32 size){ pANTLR3_STRING string; string = factory->newRaw(factory); if (string == (pANTLR3_STRING)(ANTLR3_ERR_NOMEM)) { return string; } /* Always add one more byte for a terminator ;-) */ string->chars = (pANTLR3_UINT8) ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_UINT16) * (size+1))); string->size = size+1; /* Size is always in characters, as is len */ return string;}/** Creates a new 8 bit string initialized with the 8 bit characters at the * supplied ptr, of pre-determined size. * \param[in] factory - Pointer to the string factory that owns the strings * \param[in] ptr - Pointer to 8 bit encoded characters * \return pointer to the new string */static pANTLR3_STRING newPtr8 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 ptr, ANTLR3_UINT32 size){ pANTLR3_STRING string; string = factory->newSize(factory, size); if (string == (pANTLR3_STRING)(ANTLR3_ERR_NOMEM)) { return string; } if (size <= 0) { return string; } if (ptr != NULL) { ANTLR3_MEMMOVE(string->chars, (const void *)ptr, size); *(string->chars + size) = '\0'; /* Terminate, these strings are usually used for Token streams and printing etc. */ string->len = size; } return string;}/** Creates a new 16 bit string initialized with the 8 bit characters at the * supplied 8 bit character ptr, of pre-determined size. * \param[in] factory - Pointer to the string factory that owns the strings * \param[in] ptr - Pointer to 8 bit encoded characters * \return pointer to the new string */static pANTLR3_STRING newPtr16_8 (pANTLR3_STRING_FACTORY factory, pANTLR3_UINT8 ptr, ANTLR3_UINT32 size){ pANTLR3_STRING string; /* newSize accepts size in characters, not bytes */ string = factory->newSize(factory, size); if (string == (pANTLR3_STRING)(ANTLR3_ERR_NOMEM)) { return string; } if (size <= 0) { return string; } if (ptr != NULL) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -