diff options
author | zxu <zxu@google.com> | 2018-01-25 13:03:07 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-25 13:03:07 -0500 |
commit | 5fdda3fa837f3138973d754f05aec7f541f806ce (patch) | |
tree | 6d440b3a7ac634c05890dade1e261aeb8ba03b31 | |
parent | 15a2926dcb986a84cf9969d7d20439d4ac2e46af (diff) |
normalize string_util (#708)
* refactoring string_util
* port string_util to iOS
-rw-r--r-- | Firestore/Example/Firestore.xcodeproj/project.pbxproj | 8 | ||||
-rw-r--r-- | Firestore/Port/string_util.h | 66 | ||||
-rw-r--r-- | Firestore/Source/Local/FSTLevelDBKey.mm | 2 | ||||
-rw-r--r-- | Firestore/Source/Local/FSTLevelDBMutationQueue.mm | 4 | ||||
-rw-r--r-- | Firestore/Source/Local/FSTLevelDBQueryCache.mm | 1 | ||||
-rw-r--r-- | Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm | 1 | ||||
-rw-r--r-- | Firestore/core/src/firebase/firestore/util/CMakeLists.txt | 2 | ||||
-rw-r--r-- | Firestore/core/src/firebase/firestore/util/string_util.cc (renamed from Firestore/Port/string_util.cc) | 18 | ||||
-rw-r--r-- | Firestore/core/src/firebase/firestore/util/string_util.h | 72 | ||||
-rw-r--r-- | Firestore/core/test/firebase/firestore/util/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Firestore/core/test/firebase/firestore/util/string_util_test.cc (renamed from Firestore/Port/string_util_test.cc) | 21 |
11 files changed, 103 insertions, 93 deletions
diff --git a/Firestore/Example/Firestore.xcodeproj/project.pbxproj b/Firestore/Example/Firestore.xcodeproj/project.pbxproj index 5db2b56..155b4f2 100644 --- a/Firestore/Example/Firestore.xcodeproj/project.pbxproj +++ b/Firestore/Example/Firestore.xcodeproj/project.pbxproj @@ -27,7 +27,6 @@ 5436F32420008FAD006E51E3 /* string_printf_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 5436F32320008FAD006E51E3 /* string_printf_test.cc */; }; 54740A571FC914BA00713A1A /* secure_random_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 54740A531FC913E500713A1A /* secure_random_test.cc */; }; 54740A581FC914F000713A1A /* autoid_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 54740A521FC913E500713A1A /* autoid_test.cc */; }; - 54764FAB1FAA0C320085E60A /* string_util_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 54764FAA1FAA0C320085E60A /* string_util_test.cc */; }; 54764FAF1FAA21B90085E60A /* FSTGoogleTestTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 54764FAE1FAA21B90085E60A /* FSTGoogleTestTests.mm */; }; 548DB927200D590300E00ABC /* assert_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 548DB926200D590300E00ABC /* assert_test.cc */; }; 548DB929200D59F600E00ABC /* comparison_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 548DB928200D59F600E00ABC /* comparison_test.cc */; }; @@ -66,6 +65,7 @@ 71719F9F1E33DC2100824A3D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 71719F9D1E33DC2100824A3D /* LaunchScreen.storyboard */; }; 873B8AEB1B1F5CCA007FD442 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 873B8AEA1B1F5CCA007FD442 /* Main.storyboard */; }; AB356EF7200EA5EB0089B766 /* field_value_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB356EF6200EA5EB0089B766 /* field_value_test.cc */; }; + AB380CFE201A2F4500D97691 /* string_util_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB380CFC201A2EE200D97691 /* string_util_test.cc */; }; AB382F7C1FE02A1F007CA955 /* FIRDocumentReferenceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AB382F7B1FE02A1F007CA955 /* FIRDocumentReferenceTests.m */; }; AB382F7E1FE03059007CA955 /* FIRFieldPathTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AB382F7D1FE03059007CA955 /* FIRFieldPathTests.m */; }; AB7BAB342012B519001E0872 /* geo_point_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB7BAB332012B519001E0872 /* geo_point_test.cc */; }; @@ -203,7 +203,6 @@ 5436F32320008FAD006E51E3 /* string_printf_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = string_printf_test.cc; path = ../../core/test/firebase/firestore/util/string_printf_test.cc; sourceTree = "<group>"; }; 54740A521FC913E500713A1A /* autoid_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = autoid_test.cc; path = ../../core/test/firebase/firestore/util/autoid_test.cc; sourceTree = "<group>"; }; 54740A531FC913E500713A1A /* secure_random_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = secure_random_test.cc; path = ../../core/test/firebase/firestore/util/secure_random_test.cc; sourceTree = "<group>"; }; - 54764FAA1FAA0C320085E60A /* string_util_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = string_util_test.cc; path = ../../Port/string_util_test.cc; sourceTree = "<group>"; }; 54764FAE1FAA21B90085E60A /* FSTGoogleTestTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = FSTGoogleTestTests.mm; path = GoogleTest/FSTGoogleTestTests.mm; sourceTree = "<group>"; }; 548DB926200D590300E00ABC /* assert_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = assert_test.cc; path = ../../core/test/firebase/firestore/util/assert_test.cc; sourceTree = "<group>"; }; 548DB928200D59F600E00ABC /* comparison_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = comparison_test.cc; path = ../../core/test/firebase/firestore/util/comparison_test.cc; sourceTree = "<group>"; }; @@ -250,6 +249,7 @@ 9D52E67EE96AA7E5D6F69748 /* Pods-Firestore_IntegrationTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Firestore_IntegrationTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Firestore_IntegrationTests/Pods-Firestore_IntegrationTests.debug.xcconfig"; sourceTree = "<group>"; }; 9EF477AD4B2B643FD320867A /* Pods-Firestore_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Firestore_Example.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Firestore_Example/Pods-Firestore_Example.debug.xcconfig"; sourceTree = "<group>"; }; AB356EF6200EA5EB0089B766 /* field_value_test.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = field_value_test.cc; sourceTree = "<group>"; }; + AB380CFC201A2EE200D97691 /* string_util_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = string_util_test.cc; path = ../../core/test/firebase/firestore/util/string_util_test.cc; sourceTree = "<group>"; }; AB382F7B1FE02A1F007CA955 /* FIRDocumentReferenceTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRDocumentReferenceTests.m; sourceTree = "<group>"; }; AB382F7D1FE03059007CA955 /* FIRFieldPathTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRFieldPathTests.m; sourceTree = "<group>"; }; AB7BAB332012B519001E0872 /* geo_point_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = geo_point_test.cc; path = ../../core/test/firebase/firestore/geo_point_test.cc; sourceTree = "<group>"; }; @@ -408,6 +408,7 @@ 54C2294E1FECABAE007D065B /* log_test.cc */, 54740A531FC913E500713A1A /* secure_random_test.cc */, 5436F32320008FAD006E51E3 /* string_printf_test.cc */, + AB380CFC201A2EE200D97691 /* string_util_test.cc */, ); name = util; sourceTree = "<group>"; @@ -427,7 +428,6 @@ 54764FAD1FAA0C650085E60A /* Port */ = { isa = PBXGroup; children = ( - 54764FAA1FAA0C320085E60A /* string_util_test.cc */, ); name = Port; sourceTree = "<group>"; @@ -1221,6 +1221,7 @@ DE51B1E11F0D490D0013853F /* FSTMemoryRemoteDocumentCacheTests.m in Sources */, DE51B1FF1F0D493A0013853F /* FSTAssertTests.m in Sources */, DE51B1D31F0D48CD0013853F /* FSTViewSnapshotTest.m in Sources */, + AB380CFE201A2F4500D97691 /* string_util_test.cc in Sources */, DE51B1F91F0D491F0013853F /* FSTWatchChangeTests.m in Sources */, DE51B1F81F0D491F0013853F /* FSTWatchChange+Testing.m in Sources */, DE51B1EB1F0D490D0013853F /* FSTWriteGroupTests.mm in Sources */, @@ -1254,7 +1255,6 @@ DE51B1FB1F0D492C0013853F /* FSTMemorySpecTests.m in Sources */, DE51B1DB1F0D490D0013853F /* FSTLevelDBQueryCacheTests.m in Sources */, AB356EF7200EA5EB0089B766 /* field_value_test.cc in Sources */, - 54764FAB1FAA0C320085E60A /* string_util_test.cc in Sources */, 54E9282C1F339CAD00C1953E /* XCTestCase+Await.m in Sources */, AB99452E1FE30AC800DFC1E6 /* FIRFieldValueTests.m in Sources */, AB7BAB342012B519001E0872 /* geo_point_test.cc in Sources */, diff --git a/Firestore/Port/string_util.h b/Firestore/Port/string_util.h deleted file mode 100644 index 6e85ba9..0000000 --- a/Firestore/Port/string_util.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2017 Google - * - * 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. - */ - -// Useful string functions and so forth. This is a grab-bag file. -// -// These functions work fine for UTF-8 strings as long as you can -// consider them to be just byte strings. For example, due to the -// design of UTF-8 you do not need to worry about accidental matches, -// as long as all your inputs are valid UTF-8 (use \uHHHH, not \xHH or \oOOO). - -#ifndef IPHONE_FIRESTORE_PORT_STRING_UTIL_H_ -#define IPHONE_FIRESTORE_PORT_STRING_UTIL_H_ - -#include <string> - -namespace leveldb { -class Slice; -} - -namespace Firestore { - -// Returns the smallest lexicographically larger string of equal or smaller -// length. Returns an empty string if there is no such successor (if the input -// is empty or consists entirely of 0xff bytes). -// Useful for calculating the smallest lexicographically larger string -// that will not be prefixed by the input string. -// -// Examples: -// "a" -> "b", "aaa" -> "aab", "aa\xff" -> "ab", "\xff" -> "", "" -> "" -std::string PrefixSuccessor(leveldb::Slice prefix); - -// Returns the immediate lexicographically-following string. This is useful to -// turn an inclusive range into something that can be used with Bigtable's -// SetLimitRow(): -// -// // Inclusive range [min_element, max_element]. -// string min_element = ...; -// string max_element = ...; -// -// // Equivalent range [range_start, range_end). -// string range_start = min_element; -// string range_end = ImmediateSuccessor(max_element); -// -// WARNING: Returns the input string with a '\0' appended; if you call c_str() -// on the result, it will compare equal to s. -// -// WARNING: Transforms "" -> "\0"; this doesn't account for Bigtable's special -// treatment of "" as infinity. -std::string ImmediateSuccessor(leveldb::Slice s); - -} // namespace Firestore - -#endif // IPHONE_FIRESTORE_PORT_STRING_UTIL_H_ diff --git a/Firestore/Source/Local/FSTLevelDBKey.mm b/Firestore/Source/Local/FSTLevelDBKey.mm index c6f51b9..074d5c5 100644 --- a/Firestore/Source/Local/FSTLevelDBKey.mm +++ b/Firestore/Source/Local/FSTLevelDBKey.mm @@ -19,14 +19,12 @@ #include <string> #include "Firestore/Port/ordered_code.h" -#include "Firestore/Port/string_util.h" #import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/Source/Model/FSTPath.h" NS_ASSUME_NONNULL_BEGIN using Firestore::OrderedCode; -using Firestore::PrefixSuccessor; using Firestore::StringView; using leveldb::Slice; diff --git a/Firestore/Source/Local/FSTLevelDBMutationQueue.mm b/Firestore/Source/Local/FSTLevelDBMutationQueue.mm index 56a22a1..74463ee 100644 --- a/Firestore/Source/Local/FSTLevelDBMutationQueue.mm +++ b/Firestore/Source/Local/FSTLevelDBMutationQueue.mm @@ -35,7 +35,7 @@ #import "Firestore/Source/Util/FSTAssert.h" #include "Firestore/Port/ordered_code.h" -#include "Firestore/Port/string_util.h" +#include "Firestore/core/src/firebase/firestore/util/string_util.h" NS_ASSUME_NONNULL_BEGIN @@ -164,7 +164,7 @@ static ReadOptions StandardReadOptions() { while (moreUserIDs) { // Compute the first key after the last mutation for nextUserID. auto userEnd = [FSTLevelDBMutationKey keyPrefixWithUserID:nextUserID]; - userEnd = Firestore::PrefixSuccessor(userEnd); + userEnd = firebase::firestore::util::PrefixSuccessor(userEnd); // Seek to that key with the intent of finding the boundary between nextUserID's mutations // and the one after that (if any). diff --git a/Firestore/Source/Local/FSTLevelDBQueryCache.mm b/Firestore/Source/Local/FSTLevelDBQueryCache.mm index 8388b96..5feb9f1 100644 --- a/Firestore/Source/Local/FSTLevelDBQueryCache.mm +++ b/Firestore/Source/Local/FSTLevelDBQueryCache.mm @@ -30,7 +30,6 @@ #import "Firestore/Source/Util/FSTAssert.h" #include "Firestore/Port/ordered_code.h" -#include "Firestore/Port/string_util.h" NS_ASSUME_NONNULL_BEGIN diff --git a/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm b/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm index 02f9f3e..039712c 100644 --- a/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm +++ b/Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm @@ -33,7 +33,6 @@ #import "Firestore/Source/Util/FSTAssert.h" #include "Firestore/Port/ordered_code.h" -#include "Firestore/Port/string_util.h" NS_ASSUME_NONNULL_BEGIN diff --git a/Firestore/core/src/firebase/firestore/util/CMakeLists.txt b/Firestore/core/src/firebase/firestore/util/CMakeLists.txt index 09db164..51621a0 100644 --- a/Firestore/core/src/firebase/firestore/util/CMakeLists.txt +++ b/Firestore/core/src/firebase/firestore/util/CMakeLists.txt @@ -113,6 +113,8 @@ cc_library( firebase_assert.h log.h secure_random.h + string_util.cc + string_util.h DEPENDS ${UTIL_DEPENDS} firebase_firestore_util_base diff --git a/Firestore/Port/string_util.cc b/Firestore/core/src/firebase/firestore/util/string_util.cc index 2587860..b7f1ed9 100644 --- a/Firestore/Port/string_util.cc +++ b/Firestore/core/src/firebase/firestore/util/string_util.cc @@ -14,19 +14,19 @@ * limitations under the License. */ -#include "Firestore/Port/string_util.h" +#include "Firestore/core/src/firebase/firestore/util/string_util.h" -#include <leveldb/db.h> +namespace firebase { +namespace firestore { +namespace util { -namespace Firestore { - -std::string PrefixSuccessor(leveldb::Slice prefix) { +std::string PrefixSuccessor(absl::string_view prefix) { // We can increment the last character in the string and be done // unless that character is 255 (0xff), in which case we have to erase the // last character and increment the previous character, unless that // is 255, etc. If the string is empty or consists entirely of // 255's, we just return the empty string. - std::string limit(prefix.data(), prefix.size()); + std::string limit(prefix); while (!limit.empty()) { size_t index = limit.length() - 1; if (limit[index] == '\xff') { // char literal avoids signed/unsigned. @@ -39,7 +39,7 @@ std::string PrefixSuccessor(leveldb::Slice prefix) { return limit; } -std::string ImmediateSuccessor(leveldb::Slice s) { +std::string ImmediateSuccessor(absl::string_view s) { // Return the input string, with an additional NUL byte appended. std::string out; out.reserve(s.size() + 1); @@ -48,4 +48,6 @@ std::string ImmediateSuccessor(leveldb::Slice s) { return out; } -} // namespace Firestore +} // namespace util +} // namespace firestore +} // namespace firebase diff --git a/Firestore/core/src/firebase/firestore/util/string_util.h b/Firestore/core/src/firebase/firestore/util/string_util.h new file mode 100644 index 0000000..3de177d --- /dev/null +++ b/Firestore/core/src/firebase/firestore/util/string_util.h @@ -0,0 +1,72 @@ +/* + * Copyright 2017 Google + * + * 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. + */ + +// Useful string functions and so forth. This is a grab-bag file. +// +// These functions work fine for UTF-8 strings as long as you can +// consider them to be just byte strings. For example, due to the +// design of UTF-8 you do not need to worry about accidental matches, +// as long as all your inputs are valid UTF-8 (use \uHHHH, not \xHH or \oOOO). + +#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_UTIL_H_ +#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_UTIL_H_ + +#include <string> + +#include "absl/strings/string_view.h" + +namespace firebase { +namespace firestore { +namespace util { + +/* + * Returns the smallest lexicographically larger string of equal or smaller + * length. Returns an empty string if there is no such successor (if the input + * is empty or consists entirely of 0xff bytes). + * Useful for calculating the smallest lexicographically larger string + * that will not be prefixed by the input string. + * + * Examples: + * "a" -> "b", "aaa" -> "aab", "aa\xff" -> "ab", "\xff" -> "", "" -> "" + */ +std::string PrefixSuccessor(absl::string_view prefix); + +/* + * Returns the immediate lexicographically-following string. This is useful to + * turn an inclusive range into something that can be used with Bigtable's + * SetLimitRow(): + * + * // Inclusive range [min_element, max_element]. + * string min_element = ...; + * string max_element = ...; + * + * // Equivalent range [range_start, range_end). + * string range_start = min_element; + * string range_end = ImmediateSuccessor(max_element); + * + * WARNING: Returns the input string with a '\0' appended; if you call c_str() + * on the result, it will compare equal to s. + * + * WARNING: Transforms "" -> "\0"; this doesn't account for Bigtable's special + * treatment of "" as infinity. + */ +std::string ImmediateSuccessor(absl::string_view s); + +} // namespace util +} // namespace firestore +} // namespace firebase + +#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_UTIL_H_ diff --git a/Firestore/core/test/firebase/firestore/util/CMakeLists.txt b/Firestore/core/test/firebase/firestore/util/CMakeLists.txt index 5e7612c..13a2e46 100644 --- a/Firestore/core/test/firebase/firestore/util/CMakeLists.txt +++ b/Firestore/core/test/firebase/firestore/util/CMakeLists.txt @@ -38,6 +38,7 @@ cc_test( autoid_test.cc comparison_test.cc string_printf_test.cc + string_util_test.cc DEPENDS firebase_firestore_util ) diff --git a/Firestore/Port/string_util_test.cc b/Firestore/core/test/firebase/firestore/util/string_util_test.cc index 331f96e..f94596f 100644 --- a/Firestore/Port/string_util_test.cc +++ b/Firestore/core/test/firebase/firestore/util/string_util_test.cc @@ -14,16 +14,15 @@ * limitations under the License. */ -#include "Firestore/Port/string_util.h" +#include "Firestore/core/src/firebase/firestore/util/string_util.h" #include <gtest/gtest.h> -#include <leveldb/db.h> -using Firestore::PrefixSuccessor; -using Firestore::ImmediateSuccessor; -using leveldb::Slice; +namespace firebase { +namespace firestore { +namespace util { -TEST(Util, PrefixSuccessor) { +TEST(StringUtil, PrefixSuccessor) { EXPECT_EQ(PrefixSuccessor("a"), "b"); EXPECT_EQ(PrefixSuccessor("aaAA"), "aaAB"); EXPECT_EQ(PrefixSuccessor("aaa\xff"), "aab"); @@ -33,7 +32,11 @@ TEST(Util, PrefixSuccessor) { EXPECT_EQ(PrefixSuccessor(""), ""); } -TEST(Util, ImmediateSuccessor) { - EXPECT_EQ(ImmediateSuccessor("hello"), Slice("hello\0", 6)); - EXPECT_EQ(ImmediateSuccessor(""), Slice("\0", 1)); +TEST(StringUtil, ImmediateSuccessor) { + EXPECT_EQ(ImmediateSuccessor("hello"), std::string("hello\0", 6)); + EXPECT_EQ(ImmediateSuccessor(""), std::string("\0", 1)); } + +} // namespace util +} // namespace firestore +} // namespace firebase |