Added StringHelper class
Change-Id: I1b9c7bff7127410619f0753d3aa2a52229d2fc85
diff --git a/Android.mk b/Android.mk
index ab8efaf..bd7a578 100644
--- a/Android.mk
+++ b/Android.mk
@@ -28,6 +28,7 @@
PredefinedType.cpp \
ScalarType.cpp \
Scope.cpp \
+ StringHelper.cpp \
StringType.cpp \
Type.cpp \
TypeDef.cpp \
diff --git a/FQName.cpp b/FQName.cpp
index bc47c46..b031c6f 100644
--- a/FQName.cpp
+++ b/FQName.cpp
@@ -15,6 +15,7 @@
*/
#include "FQName.h"
+#include "StringHelper.h"
#include <android-base/logging.h>
#include <iostream>
@@ -159,40 +160,6 @@
return string() == other.string();
}
-static void SplitString(
- const std::string &s, char c, std::vector<std::string> *components) {
- components->clear();
-
- size_t startPos = 0;
- size_t matchPos;
- while ((matchPos = s.find(c, startPos)) != std::string::npos) {
- components->push_back(s.substr(startPos, matchPos - startPos));
- startPos = matchPos + 1;
- }
-
- if (startPos + 1 < s.length()) {
- components->push_back(s.substr(startPos));
- }
-}
-
-// static
-std::string FQName::JoinStrings(
- const std::vector<std::string> &components,
- const std::string &separator) {
- std::string out;
- bool first = true;
- for (const auto &component : components) {
- if (!first) {
- out += separator;
- }
- out += component;
-
- first = false;
- }
-
- return out;
-}
-
const FQName FQName::getTopLevelType() const {
auto idx = mName.find('.');
@@ -208,11 +175,11 @@
getPackageAndVersionComponents(&components, true /* cpp_compatible */);
std::vector<std::string> nameComponents;
- SplitString(mName, '.', &nameComponents);
+ StringHelper::SplitString(mName, '.', &nameComponents);
components.insert(components.end(), nameComponents.begin(), nameComponents.end());
- return JoinStrings(components, "_");
+ return StringHelper::JoinStrings(components, "_");
}
std::string FQName::cppNamespace() const {
@@ -220,25 +187,25 @@
getPackageAndVersionComponents(&components, true /* cpp_compatible */);
std::string out = "::";
- out += JoinStrings(components, "::");
+ out += StringHelper::JoinStrings(components, "::");
return out;
}
std::string FQName::cppLocalName() const {
std::vector<std::string> components;
- SplitString(mName, '.', &components);
+ StringHelper::SplitString(mName, '.', &components);
- return JoinStrings(components, "::");
+ return StringHelper::JoinStrings(components, "::");
}
std::string FQName::cppName() const {
std::string out = cppNamespace();
std::vector<std::string> components;
- SplitString(name(), '.', &components);
+ StringHelper::SplitString(name(), '.', &components);
out += "::";
- out += JoinStrings(components, "::");
+ out += StringHelper::JoinStrings(components, "::");
return out;
}
@@ -247,7 +214,7 @@
std::vector<std::string> components;
getPackageAndVersionComponents(&components, true /* cpp_compatible */);
- return JoinStrings(components, ".");
+ return StringHelper::JoinStrings(components, ".");
}
std::string FQName::javaName() const {
@@ -255,7 +222,7 @@
}
void FQName::getPackageComponents(std::vector<std::string> *components) const {
- SplitString(package(), '.', components);
+ StringHelper::SplitString(package(), '.', components);
}
void FQName::getPackageAndVersionComponents(
diff --git a/FQName.h b/FQName.h
index 2fe7963..a4a8565 100644
--- a/FQName.h
+++ b/FQName.h
@@ -116,10 +116,6 @@
std::vector<std::string> *components,
bool cpp_compatible) const;
- static std::string JoinStrings(
- const std::vector<std::string> &components,
- const std::string &separator);
-
private:
bool mValid;
std::string mPackage;
diff --git a/StringHelper.cpp b/StringHelper.cpp
new file mode 100644
index 0000000..d36d942
--- /dev/null
+++ b/StringHelper.cpp
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "StringHelper.h"
+
+namespace android {
+
+// static
+std::string StringHelper::Upcase(const std::string &in) {
+ std::string out{in};
+
+ for (auto &ch : out) {
+ ch = toupper(ch);
+ }
+
+ return out;
+}
+
+// static
+void StringHelper::SplitString(
+ const std::string &s, char c, std::vector<std::string> *components) {
+ components->clear();
+
+ size_t startPos = 0;
+ size_t matchPos;
+ while ((matchPos = s.find(c, startPos)) != std::string::npos) {
+ components->push_back(s.substr(startPos, matchPos - startPos));
+ startPos = matchPos + 1;
+ }
+
+ if (startPos + 1 < s.length()) {
+ components->push_back(s.substr(startPos));
+ }
+}
+
+// static
+std::string StringHelper::JoinStrings(
+ const std::vector<std::string> &components,
+ const std::string &separator) {
+ std::string out;
+ bool first = true;
+ for (const auto &component : components) {
+ if (!first) {
+ out += separator;
+ }
+ out += component;
+
+ first = false;
+ }
+
+ return out;
+}
+
+} // namespace android
+
diff --git a/StringHelper.h b/StringHelper.h
new file mode 100644
index 0000000..a6e61d6
--- /dev/null
+++ b/StringHelper.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef STRING_HELPER_H_
+
+#define STRING_HELPER_H_
+
+#include <string>
+#include <android-base/macros.h>
+#include <vector>
+
+namespace android {
+
+struct StringHelper {
+
+ static std::string Upcase(const std::string &in);
+
+ static void SplitString(
+ const std::string &s,
+ char c,
+ std::vector<std::string> *components);
+
+ static std::string JoinStrings(
+ const std::vector<std::string> &components,
+ const std::string &separator);
+
+private:
+ StringHelper() {}
+
+ DISALLOW_COPY_AND_ASSIGN(StringHelper);
+};
+
+} // namespace android
+
+#endif // STRING_HELPER_H_
+
diff --git a/generateCpp.cpp b/generateCpp.cpp
index f11137c..2ad8019 100644
--- a/generateCpp.cpp
+++ b/generateCpp.cpp
@@ -23,6 +23,7 @@
#include "Method.h"
#include "ScalarType.h"
#include "Scope.h"
+#include "StringHelper.h"
#include <algorithm>
#include <android-base/logging.h>
@@ -31,16 +32,6 @@
namespace android {
-static std::string upcase(const std::string in) {
- std::string out{in};
-
- for (auto &ch : out) {
- ch = toupper(ch);
- }
-
- return out;
-}
-
status_t AST::generateCpp(const std::string &outputPath) const {
status_t err = generateInterfaceHeader(outputPath);
@@ -369,7 +360,7 @@
bool first = true;
for (const auto &method : iface->methods()) {
- out << upcase(method->name());
+ out << StringHelper::Upcase(method->name());
if (first) {
out << " = ";
@@ -890,7 +881,7 @@
<< "::IHw"
<< superInterface->getBaseName()
<< "::Call::"
- << upcase(method->name())
+ << StringHelper::Upcase(method->name())
<< ", _hidl_data, &_hidl_reply";
if (method->isOneway()) {
out << ", ::android::hardware::IBinder::FLAG_ONEWAY";
@@ -1036,7 +1027,7 @@
<< "::IHw"
<< superInterface->getBaseName()
<< "::Call::"
- << upcase(method->name())
+ << StringHelper::Upcase(method->name())
<< ":\n{\n";
out.indent();
diff --git a/generateCppImpl.cpp b/generateCppImpl.cpp
index 6c23e90..2e98153 100644
--- a/generateCppImpl.cpp
+++ b/generateCppImpl.cpp
@@ -33,16 +33,6 @@
namespace android {
-static std::string upcase(const std::string &in) {
- std::string out{in};
-
- for (auto &ch : out) {
- ch = toupper(ch);
- }
-
- return out;
-}
-
status_t AST::generateCppImpl(const std::string &outputPath) const {
status_t err = generateStubImplHeader(outputPath);
diff --git a/generateJava.cpp b/generateJava.cpp
index bc75547..90fb101 100644
--- a/generateJava.cpp
+++ b/generateJava.cpp
@@ -21,21 +21,12 @@
#include "Interface.h"
#include "Method.h"
#include "Scope.h"
+#include "StringHelper.h"
#include <android-base/logging.h>
namespace android {
-static std::string upcase(const std::string in) {
- std::string out{in};
-
- for (auto &ch : out) {
- ch = toupper(ch);
- }
-
- return out;
-}
-
void AST::emitJavaReaderWriter(
Formatter &out,
const std::string &parcelObj,
@@ -235,7 +226,7 @@
size_t index = 0;
for (const auto &method : iface->methods()) {
out << "public static final int kOp_"
- << upcase(method->name())
+ << StringHelper::Upcase(method->name())
<< " = "
<< base;
@@ -374,7 +365,7 @@
out << "\nHwParcel reply = new HwParcel();\n"
<< "mRemote.transact(kOp_"
- << upcase(method->name())
+ << StringHelper::Upcase(method->name())
<< ", request, reply, ";
if (method->isOneway()) {
@@ -479,7 +470,7 @@
<< superInterface->fullJavaName()
<< ".kOp_"
<<
- upcase(method->name())
+ StringHelper::Upcase(method->name())
<< ":\n{\n";
out.indent();
diff --git a/generateVts.cpp b/generateVts.cpp
index 4f3585f..dbca6b2 100644
--- a/generateVts.cpp
+++ b/generateVts.cpp
@@ -29,12 +29,6 @@
namespace android {
-// Remove the double quotas in a string.
-static std::string removeQuotes(const std::string in) {
- std::string out{in};
- return out.substr(1, out.size() - 2);
-}
-
status_t AST::emitVtsTypeDeclarations(Formatter &out) const {
std::set<AST *> allImportedASTs;
return emitVtsTypeDeclarationsHelper(out, &allImportedASTs);