Import protobuf-codegen-2.14.0
* Add OWNERS, Android.bp, and README.android.
* Hard code version number in src/lib.rs for now.
It could be in a smarter update_package.sh to get the
new version number from Cargo.tom. But until then,
the difference in lib.rs will be caught and fixed manually.
* Rename protoc_gen_rust to protoc-gen-rust for aprotoc plugin.
Bug: 143953733
Test: make
Change-Id: I9b3c3b9f2e7ad0eb203c26534f2b6ba5fac46eef
diff --git a/src/strx.rs b/src/strx.rs
new file mode 100644
index 0000000..e822bf8
--- /dev/null
+++ b/src/strx.rs
@@ -0,0 +1,54 @@
+pub fn remove_to<'s>(s: &'s str, c: char) -> &'s str {
+ match s.rfind(c) {
+ Some(pos) => &s[(pos + 1)..],
+ None => s,
+ }
+}
+
+pub fn remove_suffix<'s>(s: &'s str, suffix: &str) -> &'s str {
+ if !s.ends_with(suffix) {
+ s
+ } else {
+ &s[..(s.len() - suffix.len())]
+ }
+}
+
+pub fn capitalize(s: &str) -> String {
+ if s.is_empty() {
+ return String::new();
+ }
+ let mut char_indices = s.char_indices();
+ char_indices.next().unwrap();
+ match char_indices.next() {
+ None => s.to_uppercase(),
+ Some((i, _)) => s[..i].to_uppercase() + &s[i..],
+ }
+}
+
+#[cfg(test)]
+mod test {
+
+ use super::capitalize;
+ use super::remove_suffix;
+ use super::remove_to;
+
+ #[test]
+ fn test_remove_to() {
+ assert_eq!("aaa", remove_to("aaa", '.'));
+ assert_eq!("bbb", remove_to("aaa.bbb", '.'));
+ assert_eq!("ccc", remove_to("aaa.bbb.ccc", '.'));
+ }
+
+ #[test]
+ fn test_remove_suffix() {
+ assert_eq!("bbb", remove_suffix("bbbaaa", "aaa"));
+ assert_eq!("aaa", remove_suffix("aaa", "bbb"));
+ }
+
+ #[test]
+ fn test_capitalize() {
+ assert_eq!("", capitalize(""));
+ assert_eq!("F", capitalize("f"));
+ assert_eq!("Foo", capitalize("foo"));
+ }
+}