示例#1
0
void Tokenize( const tstring& str, std::vector< T >& tokens, const tstring& delimiters )
{
    // Skip delimiters at beginning.
    tstring::size_type lastPos = str.find_first_not_of( delimiters, 0 );
    // Find first "non-delimiter".
    tstring::size_type pos     = str.find_first_of( delimiters, lastPos );

    I temp;
    while ( tstring::npos != pos || tstring::npos != lastPos )
    {
        // Found a token, convert it to the proper type for our vector
        tstringstream stream (str.substr( lastPos, pos - lastPos ));
        stream >> temp; // NOTE: Stream operator stops at spaces!
        if ( !stream.fail() )
        {
            // Add the token to the vector
            tokens.push_back( (T)temp );
        }
        else
        {
            HELIUM_BREAK();
        }
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of( delimiters, pos );
        // Find next "non-delimiter"
        pos = str.find_first_of( delimiters, lastPos );
    }
}
示例#2
0
文件: Utils.cpp 项目: Ocerus/Ocerus
CTokenizer::CTokenizer(const tstring& p_Str, const tstring& p_Delimiters)
{
	const tstring::size_type len = p_Str.length();
	tstring::size_type i = 0;

	while(i < len)
	{
		// eat leading whitespace
		i = p_Str.find_first_not_of(p_Delimiters, i);
		if(i == tstring::npos)
			return;   // nothing left but white space

		// find the end of the token
		tstring::size_type j = p_Str.find_first_of(p_Delimiters, i);

		// push token
		if(j == tstring::npos) 
		{
			m_Tokens.push_back(p_Str.substr(i));
			return;
		} else
			m_Tokens.push_back(p_Str.substr(i, j - i));

		// set up for next loop
		i = j + 1;
	}
}
示例#3
0
inline void Tokenize<tstring, tstring>( const tstring& str, std::vector< tstring >& tokens, const tstring& delimiters )
{
    // Skip delimiters at beginning.
    tstring::size_type lastPos = str.find_first_not_of( delimiters, 0 );
    // Find first "non-delimiter".
    tstring::size_type pos     = str.find_first_of( delimiters, lastPos );

    while ( tstring::npos != pos || tstring::npos != lastPos )
    {
        // Add the token to the vector
        tokens.push_back( str.substr( lastPos, pos - lastPos ) );
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of( delimiters, pos );
        // Find next "non-delimiter"
        pos = str.find_first_of( delimiters, lastPos );
    }
}
示例#4
0
tstring trim(const tstring &tstr, const tstring& trimChars)
{
    size_t s = tstr.find_first_not_of(trimChars);
    size_t e = tstr.find_last_not_of (trimChars);

    if ((tstring::npos == s) || ( tstring::npos == e))
        return _T("");
    else
        return tstr.substr(s, e - s + 1);
}
示例#5
0
inline void Tokenize( const tstring& str, std::map< TKey, TVal >& tokens, const tstring& delimiters )
{
    // Skip delimiters at beginning.
    tstring::size_type lastPos = str.find_first_not_of( delimiters, 0 );
    // Find first "non-delimiter".
    tstring::size_type pos     = str.find_first_of( delimiters, lastPos );

    while ( tstring::npos != pos || tstring::npos != lastPos )
    {
        tstringstream kStream( str.substr( lastPos, pos - lastPos ) );

        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of( delimiters, pos );
        // Find next "non-delimiter"
        pos = str.find_first_of( delimiters, lastPos );

        if ( tstring::npos != pos || tstring::npos != lastPos )
        {
            tstringstream vStream( str.substr( lastPos, pos - lastPos ) );

            // At this point, we have the key and value.  Build the map entry.
            // Note that the stream operator stops at spaces.
            TKey k;
            kStream >> k;
            TVal v;
            vStream >> v;
            tokens.insert( std::map< TKey, TVal >::value_type( k, v ) );

            // Skip delimiters.  Note the "not_of"
            lastPos = str.find_first_not_of( delimiters, pos );

            // Find next "non-delimiter"
            pos = str.find_first_of( delimiters, lastPos );
        }
        else
        {
示例#6
0
void trim(tstring& str, char ch)
{
	tstring::size_type pos = str.find_first_not_of(ch);
	if (pos == tstring::npos)
	{
		str = _T("");
		return;
	}

	tstring::size_type pos2 = str.find_last_not_of(ch);
	if (pos2 != tstring::npos)
	{
		str = str.substr(pos, pos2 - pos + 1);
	}
	else
	{
		str = str.substr(pos);
	}
}
示例#7
0
/*!
    Splits \a cmd into list of arguments (space-separated).
*/
vector<tstring> CommandParser::splitCommand(const tstring & cmd)
{
    tstring space = _T(" \t\f\v\n\r");
    vector<tstring> args;

    size_t space_pos = 0, start_pos = 0;

    while (true) {
        space_pos = cmd.find_first_of(space, start_pos);
        if (space_pos == tstring::npos) {
            args.push_back(cmd.substr(start_pos, cmd.length() - start_pos));
            break;
        } else {
            args.push_back(cmd.substr(start_pos, space_pos - start_pos));
        }

        start_pos = cmd.find_first_not_of(space, space_pos);
    }

    return args;
}
示例#8
0
void Script::ParseAttributes(tstring& attributes, Control* control)
{
  INSPECT_SCOPE_TIMER( ("Attributes Script Attribute Processing") );
  
  size_t pos = 0;
  size_t end = tstring::npos;

  while (pos < attributes.length() && pos != tstring::npos)
  {
    // eat ws
    pos = attributes.find_first_not_of(LS_WHITESPACE, pos);

    // the rest is WS, abort
    if (pos == tstring::npos)
      break;

    // search for end of keyword
    end = attributes.find_first_of(LS_WHITESPACE TXT( "=" ), pos);

    // we have no symbol term, just abort
    if (end == tstring::npos)
      break;

    // copy just our symbol into a string
    tstring key (attributes.data() + pos, end - pos);

    // next tchar
    pos = end+1;

    // eat ws
    pos = attributes.find_first_not_of(LS_WHITESPACE, pos);

    // the rest is WS, abort
    if (pos == tstring::npos)
      break;

    // see if the value is directly quoted
    size_t startQuote = attributes.find_first_of( TXT( "\"" ), pos);
    size_t endQuote = attributes.find_first_of( TXT( "\"" ), startQuote+1);

    // search for end of keyword
    end = attributes.find_first_of( TXT( ";" ), pos);

    // if the semi is in the quote
    if (startQuote != tstring::npos && endQuote != tstring::npos && startQuote < end && end < endQuote)
    {
      // search for end of value not quoted
      end = attributes.find_first_of( TXT( ";" ), endQuote);
    }

    // we have no symbol term, just abort
    if (end == tstring::npos)
    {
      end = attributes.length();
    }

    // copy just our symbol into a string
    tstring value (attributes.data() + pos, end - pos);

    // next tchar
    pos = end+1;

    // trim quoted values
    {
      size_t start = value.find_first_of('\"');
      size_t finish = value.find_last_of('\"');

      if (start != tstring::npos)
      {
        if (start == finish)
        {
          value.erase(start, 1);
        }
        else if (start < finish)
        {
          value = value.substr(start + 1, finish - start - 1);
        }
      }
    }

    // insert
    control->Process(key, value);
  }
}
示例#9
0
tstring TriToken(tstring& Token){
	tstring::size_type begin = Token.find_first_not_of(_T(' '));
    if(begin == string::npos)return Token;
    tstring::size_type end = Token.find_last_not_of(_T(' '));
	return Token.substr(begin,end-begin+1);
}