source: CIVL/examples/compare/provesa/ADFirstAidKit/profile/string-utils.cc@ a389857

main test-branch
Last change on this file since a389857 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.6 KB
Line 
1#include "string-utils.h"
2
3using namespace std;
4
5string prefix (const string & str, int len)
6{
7 if (len >= 0)
8 return str.substr (0, len);
9 else
10 return str.substr (0, str.length () + len);
11}
12
13string suffix (const string & str, int len)
14{
15 if (len >= 0)
16 return str.substr (str.length () - len, len);
17 else
18 return str.substr (-len, str.length () + len);
19}
20
21vector < string > split (const string & str, char separator)
22{
23 unsigned int components = 0;
24 unsigned int i = 0;
25 unsigned int l = str.length ();
26 // Count the components
27 while (true) {
28 while (i < l && str[i] == separator)
29 i++;
30 if (i >= l)
31 break;
32 components++;
33 while (i < l && str[i] != separator)
34 i++;
35 }
36 // Allocate the vector
37 vector < string > split_list;
38 split_list.reserve(4);
39 // Split!
40 int lpos = 0, pos = 0;
41 while (true) {
42 while (pos < l && str[pos] == separator)
43 pos++;
44 if (pos >= l)
45 break;
46 lpos = pos;
47 while (pos < l && str[pos] != separator)
48 pos++;
49 split_list.push_back (str.substr (lpos, pos - lpos));
50 }
51 return split_list;
52}
53string last_part_after(const string &str, char separator)
54{
55 int pos = str.find_last_of(separator);
56 return str.substr(pos+1);
57}
Note: See TracBrowser for help on using the repository browser.