📄 antlr3string.c
字号:
ANTLR3_UINT8 newbit[32]; sprintf((char *)newbit, "%d", i); return string->append8(string, (const char *)newbit);}static pANTLR3_UINT8 addi16 (pANTLR3_STRING string, ANTLR3_INT32 i){ ANTLR3_UINT8 newbit[32]; sprintf((char *)newbit, "%d", i); return string->append8(string, (const char *)newbit);}static pANTLR3_UINT8inserti8 (pANTLR3_STRING string, ANTLR3_UINT32 point, ANTLR3_INT32 i){ ANTLR3_UINT8 newbit[32]; sprintf((char *)newbit, "%d", i); return string->insert8(string, point, (const char *)newbit);}static pANTLR3_UINT8inserti16 (pANTLR3_STRING string, ANTLR3_UINT32 point, ANTLR3_INT32 i){ ANTLR3_UINT8 newbit[32]; sprintf((char *)newbit, "%d", i); return string->insert8(string, point, (const char *)newbit);}static pANTLR3_UINT8insert8 (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit){ ANTLR3_UINT32 len; if (point >= string->len) { return string->append(string, newbit); } len = (ANTLR3_UINT32)strlen(newbit); if (len == 0) { return string->chars; } if (string->size < (string->len + len + 1)) { string->chars = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT64)(string->len + len + 1)); string->size = string->len + len + 1; } /* Move the characters we are inserting before, including the delimiter */ ANTLR3_MEMMOVE((void *)(string->chars + point + len), (void *)(string->chars + point), (ANTLR3_UINT64)(string->len - point + 1)); /* Note we copy the exact number of bytes */ ANTLR3_MEMMOVE((void *)(string->chars + point), newbit, (ANTLR3_UINT64)(len)); string->len += len; return string->chars;}static pANTLR3_UINT8insert16_8 (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit){ ANTLR3_UINT32 len; ANTLR3_UINT32 count; pANTLR3_UINT16 inPoint; if (point >= string->len) { return string->append8(string, newbit); } len = (ANTLR3_UINT32)strlen(newbit); if (len == 0) { return string->chars; } if (string->size < (string->len + len + 1)) { string->chars = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT64)(sizeof(ANTLR3_UINT16)*(string->len + len + 1))); string->size = string->len + len + 1; } /* Move the characters we are inserting before, including the delimiter */ ANTLR3_MEMMOVE((void *)(((pANTLR3_UINT16)string->chars) + point + len), (void *)(((pANTLR3_UINT16)string->chars) + point), (ANTLR3_UINT64)(sizeof(ANTLR3_UINT16)*(string->len - point + 1))); string->len += len; inPoint = ((pANTLR3_UINT16)(string->chars))+point; for (count = 0; count<len; count++) { *(inPoint + count) = (ANTLR3_UINT16)(*(newbit+count)); } return string->chars;}static pANTLR3_UINT8insert16_16 (pANTLR3_STRING string, ANTLR3_UINT32 point, const char * newbit){ ANTLR3_UINT32 len; pANTLR3_UINT16 in; if (point >= string->len) { return string->append(string, newbit); } /** First, determine the length of the input string */ in = (pANTLR3_UINT16)newbit; len = 0; while (*in++ != '\0') { len++; } if (len == 0) { return string->chars; } if (string->size < (string->len + len + 1)) { string->chars = (pANTLR3_UINT8) ANTLR3_REALLOC((void *)string->chars, (ANTLR3_UINT64)(sizeof(ANTLR3_UINT16)*(string->len + len + 1))); string->size = string->len + len + 1; } /* Move the characters we are inserting before, including the delimiter */ ANTLR3_MEMMOVE((void *)(((pANTLR3_UINT16)string->chars) + point + len), (void *)(((pANTLR3_UINT16)string->chars) + point), (ANTLR3_UINT64)(sizeof(ANTLR3_UINT16)*(string->len - point + 1))); /* Note we copy the exact number of characters */ ANTLR3_MEMMOVE((void *)(((pANTLR3_UINT16)string->chars) + point), newbit, (ANTLR3_UINT64)(sizeof(ANTLR3_UINT16)*(len))); string->len += len; return string->chars;}static pANTLR3_UINT8 setS (pANTLR3_STRING string, pANTLR3_STRING chars){ return string->set(string, (const char *)(chars->chars));}static pANTLR3_UINT8 appendS (pANTLR3_STRING string, pANTLR3_STRING newbit){ /* We may be passed an empty string, in which case we just return the current pointer */ if (newbit == NULL || newbit->len == 0 || newbit->size == 0 || newbit->chars == NULL) { return string->chars; } else { return string->append(string, (const char *)(newbit->chars)); }}static pANTLR3_UINT8 insertS (pANTLR3_STRING string, ANTLR3_UINT32 point, pANTLR3_STRING newbit){ return string->insert(string, point, (const char *)(newbit->chars));}/* Function that compares the text of a string to the supplied * 8 bit character string and returns a result a la strcmp() */static ANTLR3_UINT32 compare8 (pANTLR3_STRING string, const char * compStr){ return strcmp((const char *)(string->chars), compStr);}/* Function that compares the text of a string with the supplied character string * (which is assumed to be in the same encoding as the string itself) and returns a result * a la strcmp() */static ANTLR3_UINT32 compare16_8 (pANTLR3_STRING string, const char * compStr){ pANTLR3_UINT16 ourString; ANTLR3_UINT32 charDiff; ourString = (pANTLR3_UINT16)(string->chars); while (((ANTLR3_UCHAR)(*ourString) != '\0') && ((ANTLR3_UCHAR)(*compStr) != '\0')) { charDiff = *ourString - *compStr; if (charDiff != 0) { return charDiff; } ourString++; compStr++; } /* At this point, one of the strings was terminated */ return (ANTLR3_UINT32)((ANTLR3_UCHAR)(*ourString) - (ANTLR3_UCHAR)(*compStr));}/* Function that compares the text of a string with the supplied character string * (which is assumed to be in the same encoding as the string itself) and returns a result * a la strcmp() */static ANTLR3_UINT32 compare16_16 (pANTLR3_STRING string, const char * compStr8){ pANTLR3_UINT16 ourString; pANTLR3_UINT16 compStr; ANTLR3_UINT32 charDiff; ourString = (pANTLR3_UINT16)(string->chars); compStr = (pANTLR3_UINT16)(compStr8); while (((ANTLR3_UCHAR)(*ourString) != '\0') && ((ANTLR3_UCHAR)(*((pANTLR3_UINT16)compStr)) != '\0')) { charDiff = *ourString - *compStr; if (charDiff != 0) { return charDiff; } ourString++; compStr++; } /* At this point, one of the strings was terminated */ return (ANTLR3_UINT32)((ANTLR3_UCHAR)(*ourString) - (ANTLR3_UCHAR)(*compStr));}/* Function that compares the text of a string with the supplied string * (which is assumed to be in the same encoding as the string itself) and returns a result * a la strcmp() */static ANTLR3_UINT32 compareS (pANTLR3_STRING string, pANTLR3_STRING compStr){ return string->compare(string, (const char *)compStr->chars);}/* Function that returns the character indexed at the supplied * offset as a 32 bit character. */static ANTLR3_UCHAR charAt8 (pANTLR3_STRING string, ANTLR3_UINT32 offset){ if (offset > string->len) { return (ANTLR3_UCHAR)'\0'; } else { return (ANTLR3_UCHAR)(*(string->chars + offset)); }}/* Function that returns the character indexed at the supplied * offset as a 32 bit character. */static ANTLR3_UCHAR charAt16 (pANTLR3_STRING string, ANTLR3_UINT32 offset){ if (offset > string->len) { return (ANTLR3_UCHAR)'\0'; } else { return (ANTLR3_UCHAR)(*((pANTLR3_UINT16)(string->chars) + offset)); }}/* Function that returns a substring of the supplied string a la .subString(s,e) * in java runtimes. */static pANTLR3_STRINGsubString8 (pANTLR3_STRING string, ANTLR3_UINT32 startIndex, ANTLR3_UINT32 endIndex){ pANTLR3_STRING newStr; if (endIndex > string->len) { endIndex = string->len + 1; } newStr = string->factory->newPtr(string->factory, string->chars + startIndex, endIndex - startIndex); return newStr;}/* Returns a substring of the supplied string a la .subString(s,e) * in java runtimes. */static pANTLR3_STRINGsubString16 (pANTLR3_STRING string, ANTLR3_UINT32 startIndex, ANTLR3_UINT32 endIndex){ pANTLR3_STRING newStr; if (endIndex > string->len) { endIndex = string->len + 1; } newStr = string->factory->newPtr(string->factory, (pANTLR3_UINT8)((pANTLR3_UINT16)(string->chars) + startIndex), endIndex - startIndex); return newStr;}/* Function that can convert the characters in the string to an integer */static ANTLR3_INT32toInt32_8 (struct ANTLR3_STRING_struct * string){ return atoi((const char *)(string->chars));}/* Function that can convert the characters in the string to an integer */static ANTLR3_INT32toInt32_16 (struct ANTLR3_STRING_struct * string){ pANTLR3_UINT16 input; ANTLR3_INT32 value; ANTLR3_BOOLEAN negate; value = 0; input = (pANTLR3_UINT16)(string->chars); negate = ANTLR3_FALSE; if (*input == (ANTLR3_UCHAR)'-') { negate = ANTLR3_TRUE; input++; } else if (*input == (ANTLR3_UCHAR)'+') { input++; } while (*input != '\0' && isdigit(*input)) { value = value * 10; value += ((ANTLR3_UINT32)(*input) - (ANTLR3_UINT32)'0'); input++; } return negate ? -value : value;}/* Function that returns a pointer to an 8 bit version of the string, * which in this case is just the string as this is * 8 bit encodiing anyway. */static pANTLR3_STRING to8_8 (pANTLR3_STRING string){ return string;}/* Function that returns an 8 bit version of the string, * which in this case is returning all the 16 bit characters * narrowed back into 8 bits, with characters that are too large * replaced with '_' */static pANTLR3_STRING to8_16 (pANTLR3_STRING string){ pANTLR3_STRING newStr; ANTLR3_UINT32 i; /* Create a new 8 bit string */ newStr = newRaw8(string->factory); if (newStr == (pANTLR3_STRING)(ANTLR3_ERR_NOMEM)) { return NULL; } /* Always add one more byte for a terminator */ newStr->chars = (pANTLR3_UINT8) ANTLR3_MALLOC((size_t)(string->len + 1)); newStr->size = string->len + 1; newStr->len = string->len; /* Now copy each 16 bit charActer , making it an 8 bit character of * some sort. */ for (i=0; i<string->len; i++) { ANTLR3_UCHAR c; c = *(((pANTLR3_UINT16)(string->chars)) + i); *(newStr->chars + i) = (ANTLR3_UINT8)(c > 255 ? '_' : c); } /* Terminate */ *(newStr->chars + newStr->len) = '\0'; return newStr;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -