Skip to content

Commit 5fdda3f

Browse files
authored
normalize string_util (#708)
* refactoring string_util * port string_util to iOS
1 parent 15a2926 commit 5fdda3f

File tree

11 files changed

+103
-93
lines changed

11 files changed

+103
-93
lines changed

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
5436F32420008FAD006E51E3 /* string_printf_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 5436F32320008FAD006E51E3 /* string_printf_test.cc */; };
2828
54740A571FC914BA00713A1A /* secure_random_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 54740A531FC913E500713A1A /* secure_random_test.cc */; };
2929
54740A581FC914F000713A1A /* autoid_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 54740A521FC913E500713A1A /* autoid_test.cc */; };
30-
54764FAB1FAA0C320085E60A /* string_util_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 54764FAA1FAA0C320085E60A /* string_util_test.cc */; };
3130
54764FAF1FAA21B90085E60A /* FSTGoogleTestTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 54764FAE1FAA21B90085E60A /* FSTGoogleTestTests.mm */; };
3231
548DB927200D590300E00ABC /* assert_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 548DB926200D590300E00ABC /* assert_test.cc */; };
3332
548DB929200D59F600E00ABC /* comparison_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 548DB928200D59F600E00ABC /* comparison_test.cc */; };
@@ -66,6 +65,7 @@
6665
71719F9F1E33DC2100824A3D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 71719F9D1E33DC2100824A3D /* LaunchScreen.storyboard */; };
6766
873B8AEB1B1F5CCA007FD442 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 873B8AEA1B1F5CCA007FD442 /* Main.storyboard */; };
6867
AB356EF7200EA5EB0089B766 /* field_value_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB356EF6200EA5EB0089B766 /* field_value_test.cc */; };
68+
AB380CFE201A2F4500D97691 /* string_util_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB380CFC201A2EE200D97691 /* string_util_test.cc */; };
6969
AB382F7C1FE02A1F007CA955 /* FIRDocumentReferenceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AB382F7B1FE02A1F007CA955 /* FIRDocumentReferenceTests.m */; };
7070
AB382F7E1FE03059007CA955 /* FIRFieldPathTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AB382F7D1FE03059007CA955 /* FIRFieldPathTests.m */; };
7171
AB7BAB342012B519001E0872 /* geo_point_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB7BAB332012B519001E0872 /* geo_point_test.cc */; };
@@ -203,7 +203,6 @@
203203
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>"; };
204204
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>"; };
205205
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>"; };
206-
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>"; };
207206
54764FAE1FAA21B90085E60A /* FSTGoogleTestTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = FSTGoogleTestTests.mm; path = GoogleTest/FSTGoogleTestTests.mm; sourceTree = "<group>"; };
208207
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>"; };
209208
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 @@
250249
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>"; };
251250
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>"; };
252251
AB356EF6200EA5EB0089B766 /* field_value_test.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = field_value_test.cc; sourceTree = "<group>"; };
252+
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>"; };
253253
AB382F7B1FE02A1F007CA955 /* FIRDocumentReferenceTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRDocumentReferenceTests.m; sourceTree = "<group>"; };
254254
AB382F7D1FE03059007CA955 /* FIRFieldPathTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRFieldPathTests.m; sourceTree = "<group>"; };
255255
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 @@
408408
54C2294E1FECABAE007D065B /* log_test.cc */,
409409
54740A531FC913E500713A1A /* secure_random_test.cc */,
410410
5436F32320008FAD006E51E3 /* string_printf_test.cc */,
411+
AB380CFC201A2EE200D97691 /* string_util_test.cc */,
411412
);
412413
name = util;
413414
sourceTree = "<group>";
@@ -427,7 +428,6 @@
427428
54764FAD1FAA0C650085E60A /* Port */ = {
428429
isa = PBXGroup;
429430
children = (
430-
54764FAA1FAA0C320085E60A /* string_util_test.cc */,
431431
);
432432
name = Port;
433433
sourceTree = "<group>";
@@ -1221,6 +1221,7 @@
12211221
DE51B1E11F0D490D0013853F /* FSTMemoryRemoteDocumentCacheTests.m in Sources */,
12221222
DE51B1FF1F0D493A0013853F /* FSTAssertTests.m in Sources */,
12231223
DE51B1D31F0D48CD0013853F /* FSTViewSnapshotTest.m in Sources */,
1224+
AB380CFE201A2F4500D97691 /* string_util_test.cc in Sources */,
12241225
DE51B1F91F0D491F0013853F /* FSTWatchChangeTests.m in Sources */,
12251226
DE51B1F81F0D491F0013853F /* FSTWatchChange+Testing.m in Sources */,
12261227
DE51B1EB1F0D490D0013853F /* FSTWriteGroupTests.mm in Sources */,
@@ -1254,7 +1255,6 @@
12541255
DE51B1FB1F0D492C0013853F /* FSTMemorySpecTests.m in Sources */,
12551256
DE51B1DB1F0D490D0013853F /* FSTLevelDBQueryCacheTests.m in Sources */,
12561257
AB356EF7200EA5EB0089B766 /* field_value_test.cc in Sources */,
1257-
54764FAB1FAA0C320085E60A /* string_util_test.cc in Sources */,
12581258
54E9282C1F339CAD00C1953E /* XCTestCase+Await.m in Sources */,
12591259
AB99452E1FE30AC800DFC1E6 /* FIRFieldValueTests.m in Sources */,
12601260
AB7BAB342012B519001E0872 /* geo_point_test.cc in Sources */,

Firestore/Port/string_util.h

Lines changed: 0 additions & 66 deletions
This file was deleted.

Firestore/Source/Local/FSTLevelDBKey.mm

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@
1919
#include <string>
2020

2121
#include "Firestore/Port/ordered_code.h"
22-
#include "Firestore/Port/string_util.h"
2322
#import "Firestore/Source/Model/FSTDocumentKey.h"
2423
#import "Firestore/Source/Model/FSTPath.h"
2524

2625
NS_ASSUME_NONNULL_BEGIN
2726

2827
using Firestore::OrderedCode;
29-
using Firestore::PrefixSuccessor;
3028
using Firestore::StringView;
3129
using leveldb::Slice;
3230

Firestore/Source/Local/FSTLevelDBMutationQueue.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#import "Firestore/Source/Util/FSTAssert.h"
3636

3737
#include "Firestore/Port/ordered_code.h"
38-
#include "Firestore/Port/string_util.h"
38+
#include "Firestore/core/src/firebase/firestore/util/string_util.h"
3939

4040
NS_ASSUME_NONNULL_BEGIN
4141

@@ -164,7 +164,7 @@ + (FSTBatchID)loadNextBatchIDFromDB:(std::shared_ptr<DB>)db {
164164
while (moreUserIDs) {
165165
// Compute the first key after the last mutation for nextUserID.
166166
auto userEnd = [FSTLevelDBMutationKey keyPrefixWithUserID:nextUserID];
167-
userEnd = Firestore::PrefixSuccessor(userEnd);
167+
userEnd = firebase::firestore::util::PrefixSuccessor(userEnd);
168168

169169
// Seek to that key with the intent of finding the boundary between nextUserID's mutations
170170
// and the one after that (if any).

Firestore/Source/Local/FSTLevelDBQueryCache.mm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#import "Firestore/Source/Util/FSTAssert.h"
3131

3232
#include "Firestore/Port/ordered_code.h"
33-
#include "Firestore/Port/string_util.h"
3433

3534
NS_ASSUME_NONNULL_BEGIN
3635

Firestore/Source/Local/FSTLevelDBRemoteDocumentCache.mm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#import "Firestore/Source/Util/FSTAssert.h"
3434

3535
#include "Firestore/Port/ordered_code.h"
36-
#include "Firestore/Port/string_util.h"
3736

3837
NS_ASSUME_NONNULL_BEGIN
3938

Firestore/core/src/firebase/firestore/util/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ cc_library(
113113
firebase_assert.h
114114
log.h
115115
secure_random.h
116+
string_util.cc
117+
string_util.h
116118
DEPENDS
117119
${UTIL_DEPENDS}
118120
firebase_firestore_util_base

Firestore/Port/string_util.cc renamed to Firestore/core/src/firebase/firestore/util/string_util.cc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include "Firestore/Port/string_util.h"
17+
#include "Firestore/core/src/firebase/firestore/util/string_util.h"
1818

19-
#include <leveldb/db.h>
19+
namespace firebase {
20+
namespace firestore {
21+
namespace util {
2022

21-
namespace Firestore {
22-
23-
std::string PrefixSuccessor(leveldb::Slice prefix) {
23+
std::string PrefixSuccessor(absl::string_view prefix) {
2424
// We can increment the last character in the string and be done
2525
// unless that character is 255 (0xff), in which case we have to erase the
2626
// last character and increment the previous character, unless that
2727
// is 255, etc. If the string is empty or consists entirely of
2828
// 255's, we just return the empty string.
29-
std::string limit(prefix.data(), prefix.size());
29+
std::string limit(prefix);
3030
while (!limit.empty()) {
3131
size_t index = limit.length() - 1;
3232
if (limit[index] == '\xff') { // char literal avoids signed/unsigned.
@@ -39,7 +39,7 @@ std::string PrefixSuccessor(leveldb::Slice prefix) {
3939
return limit;
4040
}
4141

42-
std::string ImmediateSuccessor(leveldb::Slice s) {
42+
std::string ImmediateSuccessor(absl::string_view s) {
4343
// Return the input string, with an additional NUL byte appended.
4444
std::string out;
4545
out.reserve(s.size() + 1);
@@ -48,4 +48,6 @@ std::string ImmediateSuccessor(leveldb::Slice s) {
4848
return out;
4949
}
5050

51-
} // namespace Firestore
51+
} // namespace util
52+
} // namespace firestore
53+
} // namespace firebase
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2017 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// Useful string functions and so forth. This is a grab-bag file.
18+
//
19+
// These functions work fine for UTF-8 strings as long as you can
20+
// consider them to be just byte strings. For example, due to the
21+
// design of UTF-8 you do not need to worry about accidental matches,
22+
// as long as all your inputs are valid UTF-8 (use \uHHHH, not \xHH or \oOOO).
23+
24+
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_UTIL_H_
25+
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_UTIL_H_
26+
27+
#include <string>
28+
29+
#include "absl/strings/string_view.h"
30+
31+
namespace firebase {
32+
namespace firestore {
33+
namespace util {
34+
35+
/*
36+
* Returns the smallest lexicographically larger string of equal or smaller
37+
* length. Returns an empty string if there is no such successor (if the input
38+
* is empty or consists entirely of 0xff bytes).
39+
* Useful for calculating the smallest lexicographically larger string
40+
* that will not be prefixed by the input string.
41+
*
42+
* Examples:
43+
* "a" -> "b", "aaa" -> "aab", "aa\xff" -> "ab", "\xff" -> "", "" -> ""
44+
*/
45+
std::string PrefixSuccessor(absl::string_view prefix);
46+
47+
/*
48+
* Returns the immediate lexicographically-following string. This is useful to
49+
* turn an inclusive range into something that can be used with Bigtable's
50+
* SetLimitRow():
51+
*
52+
* // Inclusive range [min_element, max_element].
53+
* string min_element = ...;
54+
* string max_element = ...;
55+
*
56+
* // Equivalent range [range_start, range_end).
57+
* string range_start = min_element;
58+
* string range_end = ImmediateSuccessor(max_element);
59+
*
60+
* WARNING: Returns the input string with a '\0' appended; if you call c_str()
61+
* on the result, it will compare equal to s.
62+
*
63+
* WARNING: Transforms "" -> "\0"; this doesn't account for Bigtable's special
64+
* treatment of "" as infinity.
65+
*/
66+
std::string ImmediateSuccessor(absl::string_view s);
67+
68+
} // namespace util
69+
} // namespace firestore
70+
} // namespace firebase
71+
72+
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_UTIL_H_

Firestore/core/test/firebase/firestore/util/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ cc_test(
3838
autoid_test.cc
3939
comparison_test.cc
4040
string_printf_test.cc
41+
string_util_test.cc
4142
DEPENDS
4243
firebase_firestore_util
4344
)

Firestore/Port/string_util_test.cc renamed to Firestore/core/test/firebase/firestore/util/string_util_test.cc

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include "Firestore/Port/string_util.h"
17+
#include "Firestore/core/src/firebase/firestore/util/string_util.h"
1818

1919
#include <gtest/gtest.h>
20-
#include <leveldb/db.h>
2120

22-
using Firestore::PrefixSuccessor;
23-
using Firestore::ImmediateSuccessor;
24-
using leveldb::Slice;
21+
namespace firebase {
22+
namespace firestore {
23+
namespace util {
2524

26-
TEST(Util, PrefixSuccessor) {
25+
TEST(StringUtil, PrefixSuccessor) {
2726
EXPECT_EQ(PrefixSuccessor("a"), "b");
2827
EXPECT_EQ(PrefixSuccessor("aaAA"), "aaAB");
2928
EXPECT_EQ(PrefixSuccessor("aaa\xff"), "aab");
@@ -33,7 +32,11 @@ TEST(Util, PrefixSuccessor) {
3332
EXPECT_EQ(PrefixSuccessor(""), "");
3433
}
3534

36-
TEST(Util, ImmediateSuccessor) {
37-
EXPECT_EQ(ImmediateSuccessor("hello"), Slice("hello\0", 6));
38-
EXPECT_EQ(ImmediateSuccessor(""), Slice("\0", 1));
35+
TEST(StringUtil, ImmediateSuccessor) {
36+
EXPECT_EQ(ImmediateSuccessor("hello"), std::string("hello\0", 6));
37+
EXPECT_EQ(ImmediateSuccessor(""), std::string("\0", 1));
3938
}
39+
40+
} // namespace util
41+
} // namespace firestore
42+
} // namespace firebase

0 commit comments

Comments
 (0)