int SimpleStringTokens( SimpStringPtr str, char split, SimpStringPtr *tokens, int *tokensize) { if ( str->Str == NULL || str->Len == 0 ) { *tokens = NULL; *tokensize = 0; return FUNC_RETURN_OK; } *tokensize = 1; for( int i = 0 ; i < str->Len ; ++i ) { if ( str->Str[i] == split ) { (*tokensize)++; } } *tokens = rm_palloc0(str->Context, sizeof(SimpString) * (*tokensize)); char *prevp = str->Str; char *p = str->Str; for ( int i = 0 ; i < *tokensize ; ++i ) { while( *p != split && *p != '\0' ) p++; initSimpleString(&((*tokens)[i]), str->Context); setSimpleStringWithContent(&((*tokens)[i]), prevp, p-prevp); p++; prevp = p; } return FUNC_RETURN_OK; }
void SimpleStringCopy(SimpStringPtr str, SimpStringPtr source) { Assert( source != NULL ); Assert( str != NULL ); setSimpleStringWithContent(str, source->Str, source->Len); }
void initSimpleStringWithContent( SimpStringPtr str, MCTYPE context, char *content, int length) { Assert( str != NULL ); str->Context = context; setSimpleStringWithContent( str, content, length); }
int SimpleStringSubstring( SimpStringPtr str, int start, int end, SimpStringPtr target) { Assert( str != NULL ); Assert( target != NULL ); /* If end == -1, the substring ends till the end of source string. */ Assert( start >= 0 && ((end > 0 && end > start) || (end == -1))); int newlen = end == -1 ? str->Len-start : end-start; /* Check and alloc target string space. */ setSimpleStringWithContent( target, str->Str+start, newlen); return newlen; }
void SimpleStringReplaceFirst(SimpStringPtr str, char *oldstr, char *newstr) { char *pos = strstr(str->Str, oldstr); /* If the old string does not exist, no need to do any update. */ if ( pos == NULL ) return; SelfMaintainBufferData smb; initializeSelfMaintainBuffer(&smb, str->Context); if ( str->Str != pos ) { appendSelfMaintainBuffer(&smb, str->Str, pos - str->Str); } int oldstrlen = strlen(oldstr); appendSelfMaintainBuffer(&smb, newstr, strlen(newstr)); if ( oldstrlen + (pos - str->Str) < str->Len ) { appendSMBStr(&smb, pos + oldstrlen); } setSimpleStringWithContent(str, smb.Buffer, getSMBContentSize(&smb)); destroySelfMaintainBuffer(&smb); }