//++ ------------------------------------------------------------------------------------ // Details: Splits string into array of strings using delimiter. If multiple delimiter // are found in sequence then they are not added to the list of splits. // Type: Method. // Args: vData - (R) String data to be split up. // vDelimiter - (R) Delimiter char or text. // vwVecSplits - (W) Container of splits found in string data. // Return: size_t - Number of splits found in the string data. // Throws: None. //-- size_t CMIUtilString::Split(const CMIUtilString &vDelimiter, VecString_t &vwVecSplits) const { vwVecSplits.clear(); if (this->empty() || vDelimiter.empty()) return 0; const size_t nLen(length()); size_t nOffset(0); do { // Find first occurrence which doesn't match to the delimiter const size_t nSectionPos(FindFirstNot(vDelimiter, nOffset)); if (nSectionPos == std::string::npos) break; // Find next occurrence of the delimiter after section size_t nNextDelimiterPos(FindFirst(vDelimiter, nSectionPos)); if (nNextDelimiterPos == std::string::npos) nNextDelimiterPos = nLen; // Extract string between delimiters const size_t nSectionLen(nNextDelimiterPos - nSectionPos); const std::string strSection(substr(nSectionPos, nSectionLen)); vwVecSplits.push_back(strSection.c_str()); // Next nOffset = nNextDelimiterPos + 1; } while (nOffset < nLen); return vwVecSplits.size(); }
//++ ------------------------------------------------------------------------------------ // Details: Splits string into array of strings using delimiter. If multiple delimiter // are found in sequence then they are not added to the list of splits. // Type: Method. // Args: vData - (R) String data to be split up. // vDelimiter - (R) Delimiter char or text. // vwVecSplits - (W) Container of splits found in string data. // Return: MIuint - Number of splits found in the string data. // Throws: None. //-- MIuint CMIUtilString::Split( const CMIUtilString & vDelimiter, VecString_t & vwVecSplits ) const { vwVecSplits.clear(); if( this->empty() || vDelimiter.empty() ) return 0; MIint nPos = find( vDelimiter ); if( nPos == (MIint) std::string::npos ) { vwVecSplits.push_back( *this ); return 1; } const MIint strLen( length() ); if( nPos == strLen ) { vwVecSplits.push_back( *this ); return 1; } MIuint nAdd1( 1 ); if( (nPos > 0) && (substr( 0, nPos ) != vDelimiter) ) { nPos = 0; nAdd1 = 0; } MIint nPos2 = find( vDelimiter, nPos + 1 ); while( nPos2 != (MIint) std::string::npos ) { const MIuint len( nPos2 - nPos - nAdd1 ); const std::string strSection( substr( nPos + nAdd1, len ) ); if( strSection != vDelimiter ) vwVecSplits.push_back( strSection.c_str() ); nPos += len + 1; nPos2 = find( vDelimiter, nPos + 1 ); nAdd1 = 0; } const std::string strSection( substr( nPos, strLen - nPos ) ); if( (strSection.length() != 0) && (strSection != vDelimiter) ) vwVecSplits.push_back( strSection.c_str() ); return vwVecSplits.size(); }
//++ ------------------------------------------------------------------------------------ // Details: Splits string into array of strings using delimiter. However the string is // also considered for text surrounded by quotes. Text with quotes including the // delimiter is treated as a whole. If multiple delimiter are found in sequence // then they are not added to the list of splits. // Type: Method. // Args: vData - (R) String data to be split up. // vDelimiter - (R) Delimiter char or text. // vwVecSplits - (W) Container of splits found in string data. // Return: MIuint - Number of splits found in the string data. // Throws: None. //-- MIuint CMIUtilString::SplitConsiderQuotes( const CMIUtilString & vDelimiter, VecString_t & vwVecSplits ) const { vwVecSplits.clear(); if( this->empty() || vDelimiter.empty() ) return 0; MIint nPos = find( vDelimiter ); if( nPos == (MIint) std::string::npos ) { vwVecSplits.push_back( *this ); return 1; } const MIint strLen( length() ); if( nPos == strLen ) { vwVecSplits.push_back( *this ); return 1; } // Look for more quotes bool bHaveQuotes = false; const MIchar cQuote = '"'; MIint nPosQ = find( cQuote ); MIint nPosQ2 = (MIint) std::string::npos; if( nPosQ != (MIint) std::string::npos ) { nPosQ2 = find( cQuote, nPosQ + 1 ); bHaveQuotes = (nPosQ2 != (MIint) std::string::npos); } MIuint nAdd1( 1 ); if( (nPos > 0) && (substr( 0, nPos ) != vDelimiter) ) { nPos = 0; nAdd1 = 0; } MIint nPos2 = find( vDelimiter, nPos + 1 ); while( nPos2 != (MIint) std::string::npos ) { if( !bHaveQuotes || (bHaveQuotes && ((nPos2 > nPosQ2) || (nPos2 < nPosQ))) ) { // Extract text or quoted text const MIuint len( nPos2 - nPos - nAdd1 ); const std::string strSection( substr( nPos + nAdd1, len ) ); if( strSection != vDelimiter ) vwVecSplits.push_back( strSection.c_str() ); nPos += len + 1; nPos2 = find( vDelimiter, nPos + 1 ); nAdd1 = 0; if( bHaveQuotes && (nPos2 > nPosQ2) ) { // Reset, look for more quotes bHaveQuotes = false; nPosQ = find( cQuote, nPos ); nPosQ2 = (MIint) std::string::npos; if( nPosQ != (MIint) std::string::npos ) { nPosQ2 = find( cQuote, nPosQ + 1 ); bHaveQuotes = (nPosQ2 != (MIint) std::string::npos); } } } else { // Skip passed text in quotes nPos2 = find( vDelimiter, nPosQ2 + 1 ); } } const std::string strSection( substr( nPos, strLen - nPos ) ); if( (strSection.length() != 0) && (strSection != vDelimiter) ) vwVecSplits.push_back( strSection.c_str() ); return vwVecSplits.size(); }