Skip to content

Commit

Permalink
normalize string_util (firebase#708)
Browse files Browse the repository at this point in the history
* refactoring string_util
* port string_util to iOS
  • Loading branch information
zxu123 committed Jan 25, 2018
1 parent 15a2926 commit 5fdda3f
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 93 deletions.
8 changes: 4 additions & 4 deletions Firestore/Example/Firestore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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>"; };
Expand Down Expand Up @@ -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>"; };
Expand Down Expand Up @@ -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>";
Expand All @@ -427,7 +428,6 @@
54764FAD1FAA0C650085E60A /* Port */ = {
isa = PBXGroup;
children = (
54764FAA1FAA0C320085E60A /* string_util_test.cc */,
);
name = Port;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down
66 changes: 0 additions & 66 deletions Firestore/Port/string_util.h

This file was deleted.

2 changes: 0 additions & 2 deletions Firestore/Source/Local/FSTLevelDBKey.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions Firestore/Source/Local/FSTLevelDBMutationQueue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -164,7 +164,7 @@ + (FSTBatchID)loadNextBatchIDFromDB:(std::shared_ptr<DB>)db {
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).
Expand Down
1 change: 0 additions & 1 deletion Firestore/Source/Local/FSTLevelDBQueryCache.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions Firestore/core/src/firebase/firestore/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -48,4 +48,6 @@ std::string ImmediateSuccessor(leveldb::Slice s) {
return out;
}

} // namespace Firestore
} // namespace util
} // namespace firestore
} // namespace firebase
72 changes: 72 additions & 0 deletions Firestore/core/src/firebase/firestore/util/string_util.h
Original file line number Diff line number Diff line change
@@ -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_
1 change: 1 addition & 0 deletions Firestore/core/test/firebase/firestore/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ cc_test(
autoid_test.cc
comparison_test.cc
string_printf_test.cc
string_util_test.cc
DEPENDS
firebase_firestore_util
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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

0 comments on commit 5fdda3f

Please sign in to comment.