Skip to content

Commit

Permalink
fix handling the first and last
Browse files Browse the repository at this point in the history
  • Loading branch information
schwehr committed Oct 31, 2012
1 parent 9dca22f commit e6249eb
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions ais.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#include "ais.h"

const string nth_field(const string &str, const size_t n, const char c) {
// TODO(schwehr): handle the off the end case better
size_t pos;
size_t count;
for (pos = 0, count = 0; count < n && pos != string::npos; count+=1) {
if (pos > 0) pos += 1; // Skip past the current char that matched
if (!n) {
const size_t pos = str.find(c);
cerr << "pos: " << pos << "\n";
if (pos == string::npos)
return string("");
return str.substr(0, pos);
}

size_t pos = 0;
for (size_t count = 0; count < n && pos != string::npos; count++) {
if (pos > 0) pos += 1;
pos = str.find(c, pos);
}
if (string::npos == pos)
Expand All @@ -14,7 +20,7 @@ const string nth_field(const string &str, const size_t n, const char c) {
const size_t start = pos;
const size_t end = str.find(c, pos+1);
if (string::npos == end)
return str.substr(start);
return str.substr(start+1);
return str.substr(start+1, end-start-1);
}

Expand Down

0 comments on commit e6249eb

Please sign in to comment.