TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: All
from: Bo Simonsen
date: 2010-04-20 23:10:06
subject: A funny thing..

Hi everybody...

I had one of these bugs today where you really wanted to say "doh".. 
I've this nice line splitting routine:

-------------- C++ code --------------
void SplitLine(string s, vector& l, char delim, int times) {
  string::size_type i = 0;
  string::size_type last = 0;
  
  bool ignore = false;

  while(i < s.size()) {
    if(s[i] == '"') {

      if(!ignore) {
        ignore = true;
        last = i + 1;
      }
      else {
        string new_str = s.substr(last, i-last);
        l.push_back(new_str);
        last = i + 2;
        i += 2;
        ignore = false;
      }
    }
    else if(s[i] == delim && !ignore) {
      string new_str = s.substr(last, i - last);
      l.push_back(new_str);
      last = i;
      last++;
    }

    ++i;
    
    if(times > 0 && static_cast(l.size()) >= times) {
      break;
    } 
  }
  if(s.size() - last > 0) {
    cout << last << " " << s.size() << endl;
    string new_str = s.substr(last, s.size() - last);
    l.push_back(new_str);
  }
}
----------------------------

The problem is that this code does not work, since the last
if-block is invoked at times where it should not be invoked.

Can anybody see why? :-)

Bo

(I'll post the solution in a couple of days if I get no response :))

--- DayDream BBS/UNIX (Linux) 2.15
* Origin: The Night Express BBS, Copenhagen, nightexp.no-ip.org (2:236/100)
SEEN-BY: 10/1 11/331 34/999 103/105 120/228 128/2 187 140/1 222/2 226/0
SEEN-BY: 236/150 249/303 250/306 261/20 38 100 1404 1406 1418 266/1413
SEEN-BY: 280/1027 320/119 396/45 633/104 260 267 690/682 734 712/848 800/432
SEEN-BY: 801/161 189 2320/100 105 200 5030/1256
@PATH: 236/100 237/53 236/150 261/38 633/260 267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.