Joe Onorato | 6c9547d | 2016-09-07 18:43:49 -0700 | [diff] [blame] | 1 | |
| 2 | #include "string_utils.h" |
| 3 | #include <iostream> |
| 4 | |
| 5 | namespace android { |
| 6 | namespace javastream_proto { |
| 7 | |
| 8 | using namespace std; |
| 9 | |
| 10 | string |
| 11 | to_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 | |
| 39 | string |
| 40 | make_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 | |
| 63 | string |
| 64 | file_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 | |
| 79 | string |
| 80 | replace_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 | |
| 92 | } // namespace javastream_proto |
| 93 | } // namespace android |
| 94 | |
| 95 | |