summaryrefslogtreecommitdiff
path: root/tools/streaming_proto/string_utils.cpp
blob: bd34ab7aa44d7904ccba42e6c15fabd270a536df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

#include "string_utils.h"
#include <iostream>

namespace android {
namespace stream_proto {

using namespace std;

string
to_camel_case(const string& str)
{
    string result;
    const int N = str.size();
    result.reserve(N);
    bool capitalize_next = true;
    for (int i=0; i<N; i++) {
        char c = str[i];
        if (c == '_') {
            capitalize_next = true;
        } else {
            if (capitalize_next && c >= 'a' && c <= 'z') {
                c = 'A' + c - 'a';
                capitalize_next = false;
            } else if (c >= 'A' && c <= 'Z') {
                capitalize_next = false;
            } else if (c >= '0' && c <= '9') {
                capitalize_next = true;
            } else {
                // All other characters (e.g. non-latin) count as capital.
                capitalize_next = false;
            }
            result += c;
        }
    }
    return result;
}

string
make_constant_name(const string& str)
{
    string result;
    const int N = str.size();
    bool underscore_next = false;
    for (int i=0; i<N; i++) {
        char c = str[i];
        if (c >= 'A' && c <= 'Z') {
            if (underscore_next) {
                result += '_';
                underscore_next = false;
            }
        } else if (c >= 'a' && c <= 'z') {
            c = 'A' + c - 'a';
            underscore_next = true;
        } else if (c == '_') {
            underscore_next = false;
        }
        result += c;
    }
    return result;
}

string
file_base_name(const string& str)
{
    size_t start = str.rfind('/');
    if (start == string::npos) {
        start = 0;
    } else {
        start++;
    }
    size_t end = str.find('.', start);
    if (end == string::npos) {
        end = str.size();
    }
    return str.substr(start, end-start);
}

string
replace_string(const string& str, const char replace, const char with)
{
    string result(str);
    const int N = result.size();
    for (int i=0; i<N; i++) {
        if (result[i] == replace) {
            result[i] = with;
        }
    }
    return result;
}

vector<string>
split(const string& str, const char delimiter)
{
    vector<string> result;
    size_t base = 0, found = 0;
    while (true) {
        found = str.find_first_of(delimiter, base);
        if (found != base) {
            string part = str.substr(base, found - base);
            if (!part.empty()) {
                result.push_back(part);
            }
        }
        if (found == str.npos) break;
        base = found + 1;
    }
    return result;
}

} // namespace stream_proto
} // namespace android