summaryrefslogtreecommitdiff
path: root/tools/streaming_proto/string_utils.cpp
diff options
context:
space:
mode:
authorJoe Onorato <joeo@google.com>2016-09-07 18:43:49 -0700
committerJoe Onorato <joeo@google.com>2016-10-12 16:37:18 -0700
commit6c9547d8e1c35d7afa9bc9be11d5ff86ec60db14 (patch)
treee541af3eda20820eaa81c1e306f704046ac3ea54 /tools/streaming_proto/string_utils.cpp
parent87ff355f0a91785a44d50f8e15392781c2065f1d (diff)
Add android.util.proto package as an @TestApi.
The classes there add a way for the platform to write out protocol buffers that doesn't require lots of small objects, generate code, and extra copying. Includes the plugin for protoc to generate the constants. Test: proto cts tests Change-Id: I6385c198cecda9ac6fa533151609e3ace341af01
Diffstat (limited to 'tools/streaming_proto/string_utils.cpp')
-rw-r--r--tools/streaming_proto/string_utils.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/tools/streaming_proto/string_utils.cpp b/tools/streaming_proto/string_utils.cpp
new file mode 100644
index 000000000000..cc738c4c108e
--- /dev/null
+++ b/tools/streaming_proto/string_utils.cpp
@@ -0,0 +1,95 @@
+
+#include "string_utils.h"
+#include <iostream>
+
+namespace android {
+namespace javastream_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;
+}
+
+} // namespace javastream_proto
+} // namespace android
+
+