Example #1
0
// split given string by delims
StringArray split_text(const string &intext, const string &delims)
{
    StringArray         r;
    string::size_type   begIdx, endIdx;
    string              s;

    begIdx = intext.find_first_not_of(delims);

    while(begIdx != string::npos) {
        // search end of the word
        endIdx = intext.find_first_of(delims, begIdx);
        if( endIdx == string::npos )
            endIdx = intext.length();

        // get the sub string
        s = intext.substr(begIdx, endIdx-begIdx);
        r.push_back(s);

        // find next begin position
        begIdx = intext.find_first_not_of(delims, endIdx);
    }

    return r;
}
Example #2
0
wordvec split (const string& line, const string& delimiters) {
    wordvec words;
    size_t end = 0;

    // Loop over the string, splitting out words, and for each word
    // thus found, append it to the output wordvec.
    for (;;) {
        size_t start = line.find_first_not_of (delimiters, end);
        if (start == string::npos) break;
        end = line.find_first_of (delimiters, start);
        words.push_back (line.substr (start, end - start));
    }
    DEBUGF ('u', words);
    return words;
}
Example #3
0
      void operator() (string const & token) const
      {
        double lat, lon;

        string::size_type n = token.find(',');
        ASSERT(n != string::npos, ());
        VERIFY(strings::to_double(token.substr(0, n), lat), ());

        n = token.find_first_not_of(", ", n);
        ASSERT(n != string::npos, ());
        VERIFY(strings::to_double(token.substr(n, token.size() - n), lon), ());

        if (m_parser.m_info.SetLat(lat) && m_parser.m_info.SetLon(lon))
          m_parser.m_latPriority = m_parser.m_lonPriority = m_priority;
      }
Example #4
0
void trim(string &s)
{
string::size_type first = s.find_first_not_of(' ');
string::size_type last = s.find_last_not_of(' ');
if ((first==string::npos) && (last== string::npos)) //string contains only whitespaces
{
 s="";
 return;
}
if (first==string::npos) //there are no leading whitespaces
 first=0;
if (last==string::npos) //there are no trailing whitespaces
 last=s.length()-1;
s = s.substr(first, last-first+1);
}
Example #5
0
    // numeric pattern: / *[-+]?\d+(\.\d*)?(e[-+]?\d+)? */
    // very ugly solution
    bool isNumber(string s) {
      size_t pos = s.find_first_not_of(' ');
      if( pos == string::npos ) return false;

      if( s[pos] == '-' || s[pos] == '+' ) ++pos;
      if( pos == s.size() ) return false;

      size_t bg_of_num = pos;
      for(; pos < s.size() && isdigit(s[pos]); ++pos );

      if( pos < s.size() ) {
        if(s[pos] == '.') {
          ++pos;
          for(; pos < s.size() && isdigit(s[pos]); ++pos );
          if( bg_of_num + 1 == pos ) return false;
        }
        if(pos < s.size() && s[pos] == 'e') {
          ++pos;
          if( bg_of_num + 1 == pos ) return false;
          if( pos == s.size() ) return false;
          if( s[pos] == '-' || s[pos] == '+' ) ++pos;
          size_t bg_of_exp = pos;
          if( pos == s.size() ) return false;
          for(; pos < s.size() && isdigit(s[pos]); ++pos );
          if(bg_of_exp == pos) return false;
        }
      }

      if( pos < s.size() ) {
        pos = s.find_first_not_of(' ', pos);
        return pos == string::npos ? true : false;
      }

      return true;

    }
Example #6
0
vector<string> split(const string& str, string delimiter)
{
	vector<string> tokens;
	
    // Skip delimiters at beginning.
    string::size_type lastPos = str.find_first_not_of(delimiter, 0);

    // Find first "non-delimiter".
    string::size_type pos = str.find_first_of(delimiter, lastPos);

    while (string::npos != pos || string::npos != lastPos)
    {
        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));

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

        // Find next "non-delimiter"
        pos = str.find_first_of(delimiter, lastPos);
    }	
	
	return tokens;
}
Example #7
0
static string trim(string line)
{
	string::size_type i = line.find_last_not_of(g_whitespaces);
	if (i == string::npos)
	{
		line.clear();
		return line;
	}
	else
		line.resize(i + 1);
	i = line.find_first_not_of(g_whitespaces);
	if (i > 0)
		line.erase(0, i);
	return line;
}
Example #8
0
vector<string> tokenize( const string& input,
                         const string& delims )
{
   vector<string> tokens;

   string::size_type tokenStart, tokenEnd;

      // get the start and end of the first token
   tokenStart = input.find_first_not_of( delims, 0 );
   tokenEnd   = input.find_first_of( delims, tokenStart );

   while( !( tokenStart == string::npos && 
             tokenEnd   == string::npos ) )
   {
         // add the token to the vector.
      tokens.push_back( input.substr( tokenStart, 
                                      tokenEnd - tokenStart ) );

      tokenStart = input.find_first_not_of( delims, tokenEnd );
      tokenEnd   = input.find_first_of(     delims, tokenStart );
   }

   return tokens;
}
Example #9
0
/**
 * Read a double from an input string. The input string is
 * modified by deleting the double from it after it is read.
 *
 * @param aString input string to read from.
 * @param rNumber double that is read is returned here.
 * @return True if double was read, false if not.
 */
bool OpenSim::readDoubleFromString(string &aString, double *rNumber, bool allowNaNs)
{
   size_t i, end;
   string buffer;

   if (aString.empty())
      return false;
   // Remove leading spaces
    while(aString[0]==' ') aString.erase(0, 1);
   /* remove any characters before the number */
   i = aString.find_first_of("0123456789-.", 0);
   if (i != 0){
       if (allowNaNs){
           std::string NaNString = "NAN";
           std::string prefix = aString.substr(0, 3);
           std::transform(prefix.begin(), prefix.end(),prefix.begin(), ::toupper);
           if (prefix==NaNString){
                aString.erase(0, 3);
                *rNumber = SimTK::NaN;
                return true;
           }
       }
      aString.erase(0, i);
   }
   /* remove number from string, copy number to buffer */
   i = aString.find_first_not_of("0123456789-+.eE", 0);
   end = aString.length();
   if (i != aString.npos)
   {
      buffer.assign(aString, 0, i);
      aString.erase(0, i);
   }
   //if number is at end of string
   else
   {
      buffer.assign(aString);
      aString.erase(0, end);
   }
   /* remove any whitespace after the string, but don't remove any tabs */
   i = findFirstNonWhiteSpace(aString);
   if (i != aString.npos && (i > 0) && (aString[i-1] != '\t'))
      aString.erase(0, i);

   if (buffer.empty())
      return false;
   *rNumber = atof(buffer.c_str());
   return true;
}
Example #10
0
//Resolve and set hostname/port for connecting
bool SocketHandler::SetDomainAndPort( string domainT, int portT )
{
    if ( domainT == "" ) return false;
    if ( portT < 1 || portT > 65536 ) return false;

    int domlen = domainT.length();

    if (domlen > 250) domainT = domainT.substr(0, 250);
    my_s_addr.sin_port = htons(portT);

    //IP?
    if ( domlen >= 7 && domlen <= 15 && domainT.find_first_not_of("0123456789.") == string::npos )
    {
        LastHost = "";
        if ( inet_aton( domainT.c_str(), &my_s_addr.sin_addr ) != 0 ) return true;
        return false;
    }

    //Same host as last time, use next IP
    if ( server && LastHost == domainT )
    {
        if ( ips == 1 ) return true;

        if ( ++ip_count == ips ) ip_count = 0;
        memcpy((char *) &my_s_addr.sin_addr.s_addr, server->h_addr_list[ip_count], server->h_length);

        return true;
    }

    //Resolve host
    if ( (server = gethostbyname( domainT.c_str() )) )
    {
        //Count IPs
        for ( ips = 0; server->h_addr_list[ips] != NULL && server->h_addrtype == AF_INET && ips != 16; ips++ );

        if ( !ips ) return false;

        memcpy((char *) &my_s_addr.sin_addr.s_addr, server->h_addr_list[0], server->h_length);

        ip_count = 0;
        LastHost = domainT;

        return true;
    }

    LastHost = "";
    return false;
}
Example #11
0
bool ItermProcess::CheckIsEnd(const string &Sample)
{
	string EndLineSample = " ";  
	int Pos = Sample.find_first_not_of(EndLineSample); 
	
	if(Pos != string::npos)
	{
		return false;
	}
	else
	{
		return true;
	}


}
Example #12
0
void utils::TrimSpaces(string &str)
{
    size_t startpos = str.find_first_not_of(
            " \t"); // Find the first character position after excluding leading blank spaces
    size_t endpos = str.find_last_not_of(" \t"); // Find the first character position from reverse af

    // if all spaces or empty return an empty string
    if ((string::npos == startpos) || (string::npos == endpos))
    {
        str = "";
    }
    else
    {
        str = str.substr(startpos, endpos - startpos + 1);
    }
}
::error_code parse_error_code(const string &line)
{
    // get name
    string name = parse_name(line);

    // get code
    string::size_type index1 = line.find_first_not_of("\", \t\r\n", line.find_last_of("\""));
    string::size_type index2 = line.find(")", index1 + 1);
    if (index1 == string::npos || index2 == string::npos)
    {
        throw runtime_error("Can't parse error_codes file, error_code format error");
    }
    int code = atoi(line.substr(index1, index2 - index1).c_str());

    return (::error_code(name, code));
}
Example #14
0
LIBSBML_CPP_NAMESPACE_BEGIN
#ifdef __cplusplus

/*
 * @return s with whitespace removed from the beginning and end.
 */
static const string
trim (const string& s)
{
  static const string whitespace(" \t\r\n");

  string::size_type begin = s.find_first_not_of(whitespace);
  string::size_type end   = s.find_last_not_of (whitespace);

  return (begin == string::npos) ? std::string() : s.substr(begin, end - begin + 1);
}
string CSVSimpleParser::removeQuotesIfAny(const string &s,const std::string& rejectedChars) {
  string::size_type beginPos = s.find_first_not_of(rejectedChars);
  string::size_type endPos = s.find_last_not_of(rejectedChars);

  if (beginPos != string::npos && endPos != string::npos) {
    try {
      return s.substr(beginPos, endPos - beginPos + 1);
    }
    catch (...) {
      return s;
    }
  }
  else {
    return s;
  }
}
Example #16
0
string const trim(string const & a, char const * p)
{
	// LASSERT(p, /**/);

	if (a.empty() || !*p)
		return a;

	size_t r = a.find_last_not_of(p);
	size_t l = a.find_first_not_of(p);

	// Is this the minimal test? (lgb)
	if (r == string::npos && l == string::npos)
		return string();

	return a.substr(l, r - l + 1);
}
Example #17
0
//-------------------------------------------------------------------
bool Parser::isIdentifier(string token) {
  if(token == "create"    ||
     token == "table"     ||
     token == "integer"   ||
     token == "numeric"   ||
     token == "char"      ||
     token == "varchar"   ||
     token == "timestamp" ||
     token == "not"       ||
     token == "null"      ||
     token == "primary"   ||
     token == "key") {
    return false;
  }
  return token.find_first_not_of("abcdefghijklmnopqrstuvwxyz_1234567890") == std::string::npos;
}
Example #18
0
vector<string> RadiusDictionary::split(string instr)
{
    vector<string> ret;
    size_t end = 0, now;
    now = instr.find_first_of("#");
    if(now != string::npos) 
	instr.erase(now);

    while( (now = instr.find_first_not_of("\t ", end)) != string::npos) {
	end = instr.find_first_of("\t ", now);
	ret.push_back(instr.substr(now, end-now));
	if(end == string::npos) break;
    }
    
    return ret;
}
Example #19
0
string TagstoreConfig::Trim(string str)
{
	string::size_type pos = str.find_last_not_of(' ');
	
	if(pos != string::npos) {
		str.erase(pos + 1);
		pos = str.find_first_not_of(' ');
		if(pos != string::npos) str.erase(0, pos);
	}
	else 
	{
		str.erase(str.begin(), str.end());
	}

	return str;
}
Example #20
0
void Trim(string& str)
{
    // trim leading spaces
    size_t startpos = str.find_first_not_of(" \n\r\t");
    if( string::npos != startpos )
    {
        str = str.substr( startpos );
    }

    // trim trailing spaces
    size_t endpos = str.find_last_not_of(" \n\r\t");
    if( string::npos != endpos )
    {
        str = str.substr( 0, endpos+1 );
    }
}
Example #21
0
void resources_input::trim(string &str, string delim)
{
  size_t first = 0;

  first = str.find_first_of(delim, first);
  while (first != string::npos) {
    size_t last = str.find_first_not_of(delim, first);
    if (last != string::npos) {
      str.erase(first, last - first);
      first = str.find_first_of(delim, first);
    } else {
      str.erase(first, str.length() - first);
      first = string::npos;
    }
  }
}
Example #22
0
list<string> ConstellationSet::parseNums(string data, string searching)
{
   string vals = "ABCDEF0123456789";
   list<string> nums;

   string::size_type frontIndex = data.find(searching);
   frontIndex = data.find_first_of(vals, frontIndex+4);
   while (frontIndex!=string::npos)
   {
      string::size_type endIndex = data.find_first_not_of(vals, frontIndex);
      string numstring = data.substr(frontIndex, endIndex-frontIndex);
      nums.push_back(numstring);
      frontIndex = data.find_first_of(vals, endIndex+1);
   }
   return nums;
}
Example #23
0
string trim(const string &line, bool sharpen) {
  size_t start = line.find_first_not_of(" \t\n\r");
  if (start == string::npos || (sharpen && line[start] == '#')) {
    return "";
  } // here we must have something in the line/string
  size_t stop = sharpen ? line.find_last_of("#") // returns either # pos or npos
                        : string::npos;
  if (stop != string::npos) {
    stop--;
  }
  stop = line.find_last_not_of(" \t\n\r", stop);
  if (stop == string::npos) {
    return "";
  } // this cannot happen
  return line.substr(start, stop + 1 - start);
}
Example #24
0
void trim(string& s)
{
	// vorne Whitespaces entfernen
	int pos1 = s.find_first_not_of(WHITE_SP);
	if (pos1 > 0)
	{
		s.erase(s.begin(), s.begin() + pos1);
	}

	// hinter Whitespaces entfernen
	pos1 = s.find_last_not_of(WHITE_SP);
	if (pos1 >= 0)
	{
		s.erase(s.begin() + pos1+1, s.end());	// o.c.
	}
}
Example #25
0
bool cCharStuff::cBankerAI::BankCheck(int c, P_CHAR pBanker, const string& comm)
{
	P_CHAR pc_currchar = currchar[c];
	int beginoffset ;
	int endoffset ;
	int value =0 ;
	string value2;
	if ((beginoffset=comm.find_first_of("0123456789")) != string::npos)
	{
		if ((endoffset=comm.find_first_not_of("0123456789",beginoffset))== string::npos)
			endoffset = comm.length();
		value2= comm.substr(beginoffset,endoffset-beginoffset) ;
		value = str2num(value2) ;
	}

	int d = pc_currchar->CountBankGold();
	{
		int goldcount = value;
		if (goldcount < 5000 || goldcount > 1000000)
		{
			sprintf(temp, "%s you can only get checks worth 5000gp to 1000000gp.", pc_currchar->name.c_str());
			npctalk(c, pBanker, temp, 1);
			return false;
		}
		if (d >= goldcount)
		{
			const P_ITEM pi = Items->SpawnItem(c, pc_currchar, 1, "bank check", 0, 0x14, 0xF0, 0, 0, 0); // bank check
			if (pi != NULL)
			pi->type = 1000;
			pi->setId(0x14F0);
			pi->color = 0x0099;
			pi->priv |= 0x02;
			pi->value = goldcount;
			DeleBankItem(pc_currchar, 0x0EED, 0, goldcount);
			P_ITEM bankbox = pc_currchar->GetBankBox();
			bankbox->AddItem(pi);
			statwindow(c, pc_currchar);
			sprintf(temp, "%s your check has been placed in your bankbox, it is worth %i.", pc_currchar->name.c_str(), goldcount);
			npctalk(c, pBanker, temp, 1);
			return true;
		}
		else
			sprintf(temp, "%s you have insufficent funds!", pc_currchar->name.c_str());
		npctalk(c, pBanker, temp, 1);
		return true;
	}
}
Example #26
0
CGeometry::CGeometry(string s)
{
	m_NumOfCoordinates = 0;

	string tmp = s.substr(s.find_first_not_of(' '));
	size_t start_idx = tmp.find_first_of('<');
	size_t end_idx = tmp.find_first_of('>');

	string type_str = tmp.substr(start_idx + 1,end_idx - start_idx -1);
	
	string start_tag = "<coordinates>";
	string end_tag = "</coordinates>";
	
	start_idx = tmp.find(start_tag);
	start_idx += start_tag.length();
	end_idx = tmp.find(end_tag);

	tmp = tmp.substr(start_idx, end_idx - start_idx);
	if (type_str.compare("Point") == 0)
	{
		m_Type = POINT;
	}
	else
	{
		if (type_str.compare("LineString") == 0)
		{
			m_Type = LINE;
		}
		else
		{
			m_Type = UNKNOWN;
		}
	}

	switch (m_Type)
	{
		case POINT:
			ReadPointCoordinate(tmp);
			break;
		case LINE:
			ReadLineStringCoordinates(tmp);
			break;
		default:
			break;
	}

}
    void reverseWords(string &s) {
        stack<char> words;
        stack<char> reverse;
        
        cout << "before trim: [" << s <<"]" << endl;
        
        s.erase(0, s.find_first_not_of(' '));  //prefixing spaces
        s.erase(s.find_last_not_of(' ')+1);     // Suffixing spaces

        cout << "after trim: [" << s <<"]" << endl;
        for (int i = 0; i < s.size(); ++i) {
            words.push(s[i]);
        }
        
        string result = "";
        
        while(!words.empty()) {
            char ch = words.top();
            words.pop();
            if (ch == ' ') {
                while(words.top() == ' ') {
                    words.pop();
                }
                if (!reverse.empty()){
                    while (!reverse.empty()) {
                        result += reverse.top();
                        reverse.pop();
                    }
                    
                    // If it is not the last word, then add space
                    if (!words.empty()) {
                        result += ' ';
                    }
                }
            } else {
                //cout << "pusing " << ch << endl;
                reverse.push(ch);
            }
        }
        
        while (!reverse.empty()) {
            result += reverse.top();
            reverse.pop();
        }
        
        s = result;
    }
Example #28
0
////////////////////////////////////////////////////////////////////////
//功能:从textline中提取出tag后面的内容并将textline5截短
//参数:
//	textline:要处理的字符串
//  tag:关键字
//  strRet:返回的内容
//////////////////////////////////////////////////////////////////////////
bool TitleExtract::selText( string &textline, string &tag, string &strRet )
{
	string::size_type pos, posBegin, posEnd;
	pos = textline.find( tag );
	if( pos == string::npos )					//如果没有找到则返回
		return false;
	
	posBegin = textline.find_first_not_of(  " \t", pos + tag.length() );
	if( posBegin == string::npos )				//如果标志后面没有内容则返回
		return false;
	
	posEnd = textline.find_first_of(  " \t", posBegin );		
	strRet = textline.substr( posBegin, posEnd - posBegin );		//截取字符串
	if( posEnd != string::npos )						//将textline截断
		textline = textline.substr( posEnd );
	return true;
}
Example #29
0
bool isNumeric(const string& stringToCheck)
{
    bool negativo = false;

    if (stringToCheck.empty()) // Las cadenas vacías no son números
        return false;

    // ¿Es un número negativo?
    if (stringToCheck[0] == '-') {
        if (stringToCheck.length() == 1) // ¿Es el operador - , cosa que no es número?
            return false;
        else
            negativo = true;
    }

    return (stringToCheck.find_first_not_of("0123456789.", negativo ? 1 : 0) == string::npos);
}
static vector<string> tokenize(const string & str, const string & delim)
{
  vector<string> tokens;

  size_t p0 = 0, p1 = string::npos;
  while(p0 != string::npos)
  {
    p1 = str.find_first_of(delim, p0);
    if(p1 != p0)
    {
      string token = str.substr(p0, p1 - p0);
      tokens.push_back(token);
    }
    p0 = str.find_first_not_of(delim, p1);
  }
  return tokens;
}