1
2#include "string_utils.h"
3#include <iostream>
4
5namespace android {
6namespace stream_proto {
7
8using namespace std;
9
10string
11to_camel_case(const string& str)
12{
13    string result;
14    const int N = str.size();
15    result.reserve(N);
16    bool capitalize_next = true;
17    for (int i=0; i<N; i++) {
18        char c = str[i];
19        if (c == '_') {
20            capitalize_next = true;
21        } else {
22            if (capitalize_next && c >= 'a' && c <= 'z') {
23                c = 'A' + c - 'a';
24                capitalize_next = false;
25            } else if (c >= 'A' && c <= 'Z') {
26                capitalize_next = false;
27            } else if (c >= '0' && c <= '9') {
28                capitalize_next = true;
29            } else {
30                // All other characters (e.g. non-latin) count as capital.
31                capitalize_next = false;
32            }
33            result += c;
34        }
35    }
36    return result;
37}
38
39string
40make_constant_name(const string& str)
41{
42    string result;
43    const int N = str.size();
44    bool underscore_next = false;
45    for (int i=0; i<N; i++) {
46        char c = str[i];
47        if (c >= 'A' && c <= 'Z') {
48            if (underscore_next) {
49                result += '_';
50                underscore_next = false;
51            }
52        } else if (c >= 'a' && c <= 'z') {
53            c = 'A' + c - 'a';
54            underscore_next = true;
55        } else if (c == '_') {
56            underscore_next = false;
57        }
58        result += c;
59    }
60    return result;
61}
62
63string
64file_base_name(const string& str)
65{
66    size_t start = str.rfind('/');
67    if (start == string::npos) {
68        start = 0;
69    } else {
70        start++;
71    }
72    size_t end = str.find('.', start);
73    if (end == string::npos) {
74        end = str.size();
75    }
76    return str.substr(start, end-start);
77}
78
79string
80replace_string(const string& str, const char replace, const char with)
81{
82    string result(str);
83    const int N = result.size();
84    for (int i=0; i<N; i++) {
85        if (result[i] == replace) {
86            result[i] = with;
87        }
88    }
89    return result;
90}
91
92vector<string>
93split(const string& str, const char delimiter)
94{
95    vector<string> result;
96    size_t base = 0, found = 0;
97    while (true) {
98        found = str.find_first_of(delimiter, base);
99        if (found != base) {
100            string part = str.substr(base, found - base);
101            if (!part.empty()) {
102                result.push_back(part);
103            }
104        }
105        if (found == str.npos) break;
106        base = found + 1;
107    }
108    return result;
109}
110
111string
112stripPrefix(const string& str, const string& prefix)
113{
114    if (str.size() <= prefix.size()) return str;
115    size_t i = 0, len = prefix.size();
116    for (; i<len; i++) {
117        if (str[i] != prefix[i]) return str;
118    }
119    return str.substr(i);
120}
121
122} // namespace stream_proto
123} // namespace android
124
125
126