// Break up a string composed of multiple sections in parentheses into a collection // of sub-strings. Sample input string: (Hello)(World1,World2) MgStringCollection* WfsGetFeatureParams::GetParenthesisedList(CREFSTRING sourceString) { MgStringCollection* stringList = new MgStringCollection(); if(sourceString.length() > 0) { // Create a collection of strings STRING remaining = MgUtil::Trim(sourceString); while(remaining.length() > 0) { STRING::size_type openParenthesis = remaining.find_first_of(L"("); if(openParenthesis != string::npos) { STRING::size_type closeParenthesis = remaining.find_first_of(L")"); if(closeParenthesis != string::npos) { STRING thisString = remaining.substr(openParenthesis + 1, closeParenthesis - openParenthesis - 1); stringList->Add(thisString); remaining = remaining.substr(closeParenthesis + 1); } } else { stringList->Add(remaining); break; } } } return stringList; }
/*! * Add escaping to each quote character (") by preceding it with a backslash (\). */ void EscapeQuotes() { size_t quote = _quoted.find_first_of('"'); while (quote != STRING::npos) { _quoted.insert(quote, 1, '\\'); quote = _quoted.find_first_of('"', quote + 2); } }
/*! * @param[in] arg The string that needs to be quoted. * @param[in] whitespace The set of characters that are considered whitespace. */ QUOTE_ARGUMENT_MS_BASE(const STRING &arg, const T *whitespace) { // Quoting is only necessary if the argument contains whitespace or a quote ("). // _quoted = arg; if (_quoted.find_first_of(whitespace) != STRING::npos || _quoted.find_first_of('"') != STRING::npos) { EscapeBackSlashes(); EscapeQuotes(); AddQuotes(whitespace); } }
int LPID::SetPartName( const STRING& aPartName ) { STRING category; STRING base; int offset; int separation = int( aPartName.find_first_of( "/" ) ); if( separation != -1 ) { category = aPartName.substr( 0, separation ); base = aPartName.substr( separation+1 ); } else { // leave category empty base = aPartName; } if( (offset = SetCategory( category )) != -1 ) return offset; if( (offset = SetBaseName( base )) != -1 ) { return offset + separation + 1; } return -1; }
/*! * Add escaping to each sequence of backslashes that immediately precedes * a quote character. Each backslash in such a sequence must be escaped * with another backslash. Note that backslashes that are not part of a * sequence that immediately precedes a quote are NOT escaped. E.g.: * * \a" -> \a" * foo\bar"baz\\"fum -> foo\bar"baz\\\\"fum */ void EscapeBackSlashes() { size_t quote = _quoted.find_first_of('"', 1); while (quote != STRING::npos) { size_t numSlashes = 0; size_t notSlash = _quoted.find_last_not_of('\\', quote-1); if (notSlash != quote-1) { if (notSlash == STRING::npos) numSlashes = quote; else numSlashes = quote - notSlash - 1; _quoted.insert(quote, numSlashes, '\\'); } quote = _quoted.find_first_of('"', quote + numSlashes + 1); } }
static inline int okBase( const STRING& aField ) { int offset = int( aField.find_first_of( ":/" ) ); if( offset != -1 ) return offset; // cannot be empty if( !aField.size() ) return 0; return offset; // ie. -1 }
/*! * Add quotes around the string if it contains any whitespace. */ void AddQuotes(const T *whitespace) { if (_quoted.find_first_of(whitespace) != STRING::npos) { _quoted.insert(0, 1, '"'); _quoted.append(1, '"'); // If the last character (prior to adding the quotes) was a backslash, // it needs to be escaped now because it precedes a quote. // size_t quote = _quoted.size() - 1; if (_quoted[quote-1] == '\\') { size_t notSlash = _quoted.find_last_not_of('\\', quote-2); size_t numSlashes = quote - notSlash - 1; _quoted.insert(quote, numSlashes, '\\'); } } }
static inline int okLogical( const STRING& aField ) { // std::string::npos is largest positive number, casting to int makes it -1. // Returning that means success. return int( aField.find_first_of( ":/" ) ); }
STRING LPID::Format( const STRING& aLogicalLib, const STRING& aPartName, const STRING& aRevision ) throw( PARSE_ERROR ) { STRING ret; int offset; if( aLogicalLib.size() ) { offset = okLogical( aLogicalLib ); if( offset != -1 ) { THROW_PARSE_ERROR( _( "Illegal character found in logical lib name" ), wxString::FromUTF8( aLogicalLib.c_str() ), aLogicalLib.c_str(), 0, offset ); } ret += aLogicalLib; ret += ':'; } { STRING category; STRING base; int separation = int( aPartName.find_first_of( "/" ) ); if( separation != -1 ) { category = aPartName.substr( 0, separation ); base = aPartName.substr( separation+1 ); } else { // leave category empty base = aPartName; } if( (offset = okCategory( category )) != -1 ) { THROW_PARSE_ERROR( _( "Illegal character found in category" ), wxString::FromUTF8( aRevision.c_str() ), aRevision.c_str(), 0, offset ); } if( (offset = okBase( base )) != -1 ) { THROW_PARSE_ERROR( _( "Illegal character found in base name" ), wxString::FromUTF8( aRevision.c_str() ), aRevision.c_str(), 0, offset + separation + 1 ); } if( category.size() ) { ret += category; ret += '/'; } ret += base; } if( aRevision.size() ) { offset = okRevision( aRevision ); if( offset != -1 ) { THROW_PARSE_ERROR( _( "Illegal character found in revision" ), wxString::FromUTF8( aRevision.c_str() ), aRevision.c_str(), 0, offset ); } ret += '/'; ret += aRevision; } return ret; }
static inline int okCategory( const STRING& aField ) { return int( aField.find_first_of( ":/" ) ); }
// 字符串相关 INT KLU_ConvertStringToVector(LPCTSTR strStrintgSource, std::vector< STRING >& vRet, LPCTSTR szKey, BOOL bOneOfKey, BOOL bIgnoreEmpty) { vRet.clear(); //------------------------------------------------------------ //合法性 if(!strStrintgSource || strStrintgSource[0] == '\0') return 0; STRING strSrc = strStrintgSource; //------------------------------------------------------------ //查找第一个分割点 STRING::size_type nLeft = 0; STRING::size_type nRight; if(bOneOfKey) { nRight = strSrc.find_first_of(szKey); } else { nRight = strSrc.find(szKey); } if(nRight == std::string::npos) { nRight = strSrc.length(); } while(TRUE) { STRING strItem = strSrc.substr(nLeft, nRight-nLeft); if(strItem.length() > 0 || !bIgnoreEmpty) { vRet.push_back(strItem); } if(nRight == strSrc.length()) { break; } nLeft = nRight + (bOneOfKey ? 1 : _tcslen(szKey)); if(bOneOfKey) { STRING strTemp = strSrc.substr(nLeft); nRight = strTemp.find_first_of(szKey); if(nRight != STRING::npos) nRight += nLeft; } else { nRight = strSrc.find(szKey, nLeft); } if(nRight == std::string::npos) { nRight = strSrc.length(); } } return (INT)vRet.size(); }
VOID CStringFilter::ReplaceToSign(const STRING& strIn, STRING& strOut) { const CHAR KeyStart = '#'; const CHAR ContentsEnd = '}'; std::vector<PICE> vSrcPice; //来源字串分割成多段 STRING strSrc = strIn; PICE pc; STRING::size_type sB = 0; STRING::size_type sC = 0; STRING::size_type sE = strSrc.find_first_of(KeyStart); STRING::size_type sLen = strSrc.size(); #if 0 ::OutputDebugString("ReplaceToSign Begin=======================\n"); char dbgmsg[256]; _snprintf(dbgmsg, 255, "strSrc:%s", strSrc.c_str()); ::OutputDebugString(dbgmsg); ::OutputDebugString("\n------------------------------------------"); #endif do { if(sE == STRING::npos) { //push last replace str pc.bReplace = TRUE; pc.pice = strSrc.substr(sC); vSrcPice.push_back(pc); break; } //get op STRING strOp = strSrc.substr(sE+1, 1); if(strOp == "{") //ok, check magic #{} string. { //item element is valid. ex: #{_INFOID123} STRING strItemElement = strSrc.substr(sE+2, 7); //info message is valid. ex: #{_INFOMSGxxxxxx} STRING strInfoMsg = strSrc.substr(sE+2, 8); if(strItemElement == "_INFOID") { //get itemId //todo_yangjun 需要仔细检查剩下的字符是否还是一个完整的INFOID信息 STRING::size_type sIDEnd = strSrc.find(ContentsEnd, sE+2+7); if(sE+2+7 >= sLen) // fix dead loop if str is "xxx#{_INFOID" [9/25/2006] { //skip invalid #{ sE += 2; goto LengthOver2; } STRING strId = strSrc.substr(sE+2+7, sIDEnd-sE-2-7); INT itemId = atoi(strId.c_str()); if(g_pTransferItemSystem->IsElementExist(itemId)) {//ok, valid item element found. //0. push normal replace str pc.bReplace = TRUE; pc.pice = strSrc.substr(sC, sE-sC); vSrcPice.push_back(pc); //1. push no replace str pc.bReplace = FALSE; pc.pice = strSrc.substr(sE, sIDEnd-sE+1); vSrcPice.push_back(pc); } //step to new point. sE = sIDEnd + 1; sC = sE; } else if(strInfoMsg == "_INFOMSG") { //get info message INT nContentsLen = atoi(strSrc.substr(sE+2+8,3).c_str()); STRING::size_type sIDEnd = sE+2+8+3+nContentsLen; if(sE+2+8 >= sLen) { //skip invalid #{ sE += 2; goto LengthOver2; } //ok, valid info message found. //0. push normal replace str pc.bReplace = TRUE; pc.pice = strSrc.substr(sC, sE-sC); vSrcPice.push_back(pc); //1. push no replace str pc.bReplace = FALSE; pc.pice = strSrc.substr(sE, sIDEnd-sE+1); vSrcPice.push_back(pc); //step to new point. sE = sIDEnd + 1; sC = sE; } else { //all other things sE += 2; } } else { //single # sE += 1; } LengthOver2: if(sE >= sLen) { if(sC != sE) { //push last replace str pc.bReplace = TRUE; pc.pice = strSrc.substr(sC); vSrcPice.push_back(pc); } break; } //save new begin point sB = sE; //find next KeyStart sE = strSrc.find(KeyStart, sB); }while(TRUE); //替换字串中的非法字符 for(INT i = 0; i < (INT)vSrcPice.size(); ++i) { #if 0 _snprintf(dbgmsg, 255, "vSrcPice[%d]:%s\n", i, vSrcPice[i].pice.c_str()); ::OutputDebugString(dbgmsg); #endif if(TRUE == vSrcPice[i].bReplace) { STRING strOld = vSrcPice[i].pice; ReplaceToSign_Normal(strOld, vSrcPice[i].pice); } } //生成结果字串 strOut.clear(); for(INT i = 0; i < (INT)vSrcPice.size(); ++i) { strOut += vSrcPice[i].pice; } #if 0 ::OutputDebugString("ReplaceToSign End=========================\n"); #endif }