Skip to content

Port StringPrintf from //base #624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,4 @@ include(external/FirebaseCore)

include(external/googletest)
include(external/leveldb)
include(external/abseil-cpp)
include(external/firestore)
1 change: 1 addition & 0 deletions Firestore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ if(APPLE)
endif(APPLE)

enable_testing()
add_subdirectory(third_party/abseil-cpp EXCLUDE_FROM_ALL)
add_subdirectory(core)
6 changes: 6 additions & 0 deletions Firestore/Example/Firestore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

/* Begin PBXBuildFile section */
3B843E4C1F3A182900548890 /* remote_store_spec_test.json in Resources */ = {isa = PBXBuildFile; fileRef = 3B843E4A1F3930A400548890 /* remote_store_spec_test.json */; };
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 */; };
Expand Down Expand Up @@ -186,6 +187,7 @@
3B843E4A1F3930A400548890 /* remote_store_spec_test.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = remote_store_spec_test.json; sourceTree = "<group>"; };
42491D7DC8C8CD245CC22B93 /* Pods-SwiftBuildTest.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SwiftBuildTest.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SwiftBuildTest/Pods-SwiftBuildTest.debug.xcconfig"; sourceTree = "<group>"; };
4EBC5F5ABE1FD097EFE5E224 /* Pods-Firestore_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Firestore_Example.release.xcconfig"; path = "Pods/Target Support Files/Pods-Firestore_Example/Pods-Firestore_Example.release.xcconfig"; sourceTree = "<group>"; };
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>"; };
Expand Down Expand Up @@ -376,6 +378,7 @@
54740A521FC913E500713A1A /* autoid_test.cc */,
54C2294E1FECABAE007D065B /* log_test.cc */,
54740A531FC913E500713A1A /* secure_random_test.cc */,
5436F32320008FAD006E51E3 /* string_printf_test.cc */,
);
name = util;
sourceTree = "<group>";
Expand Down Expand Up @@ -1186,6 +1189,7 @@
DE51B1F41F0D491B0013853F /* FSTRemoteEventTests.m in Sources */,
54E928241F33953300C1953E /* FSTEventAccumulator.m in Sources */,
DE51B1D11F0D48CD0013853F /* FSTTargetIDGeneratorTests.m in Sources */,
5436F32420008FAD006E51E3 /* string_printf_test.cc in Sources */,
DE51B1EF1F0D49140013853F /* FSTDocumentTests.m in Sources */,
DE51B1DC1F0D490D0013853F /* FSTLocalSerializerTests.m in Sources */,
DE51B1E71F0D490D0013853F /* FSTRemoteDocumentChangeBufferTests.m in Sources */,
Expand Down Expand Up @@ -1454,6 +1458,7 @@
HEADER_SEARCH_PATHS = (
"$(inherited)",
"\"${PODS_ROOT}/../../..\"",
"\"${PODS_ROOT}/../../../Firestore/third_party/abseil-cpp\"",
"\"${PODS_ROOT}/leveldb-library/include\"",
"\"${PODS_ROOT}/GoogleTest/googletest/include\"",
);
Expand Down Expand Up @@ -1486,6 +1491,7 @@
HEADER_SEARCH_PATHS = (
"$(inherited)",
"\"${PODS_ROOT}/../../..\"",
"\"${PODS_ROOT}/../../../Firestore/third_party/abseil-cpp\"",
"\"${PODS_ROOT}/leveldb-library/include\"",
"\"${PODS_ROOT}/GoogleTest/googletest/include\"",
);
Expand Down
41 changes: 31 additions & 10 deletions Firestore/core/src/firebase/firestore/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,71 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# firebase_firestore_util is the interface of this module. The rest of the
# libraries in here are an implementation detail of making this a
# mutli-platform build.

add_library(
firebase_firestore_util
autoid.cc
firebase_firestore_util_base
secure_random_arc4random.cc
string_printf.cc
)
target_link_libraries(
firebase_firestore_util_base
PRIVATE
absl_base
)

# log_stdio can be built and tested everywhere
# stdio-dependent bits can be built and tested everywhere
add_library(
firebase_firestore_util_log_stdio
firebase_firestore_util_stdio
log_stdio.cc
)
target_link_libraries(
firebase_firestore_util_stdio
PUBLIC
firebase_firestore_util_base
)

# log_apple can only built and tested on apple plaforms
# apple-dependent bits can only built and tested on apple plaforms
if(APPLE)
add_library(
firebase_firestore_util_log_apple
firebase_firestore_util_apple
log_apple.mm
)
target_compile_options(
firebase_firestore_util_log_apple
firebase_firestore_util_apple
PRIVATE
${OBJC_FLAGS}
)
target_link_libraries(
firebase_firestore_util_log_apple
firebase_firestore_util_apple
PUBLIC
FirebaseCore
)
endif(APPLE)

add_library(
firebase_firestore_util
autoid.cc
)

# Export a dependency on the correct logging library for this platform. All
# buildable libraries are built and tested but only the best fit is exported.
if(APPLE)
target_link_libraries(
firebase_firestore_util
PUBLIC
firebase_firestore_util_log_apple
firebase_firestore_util_apple
firebase_firestore_util_base
)

else(NOT APPLE)
target_link_libraries(
firebase_firestore_util
PUBLIC
firebase_firestore_util_log_stdio
firebase_firestore_util_stdio
firebase_firestore_util_base
)

endif(APPLE)
101 changes: 101 additions & 0 deletions Firestore/core/src/firebase/firestore/util/string_printf.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2018 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.
*/

#include "Firestore/core/src/firebase/firestore/util/string_printf.h"

#include <stdio.h>

namespace firebase {
namespace firestore {
namespace util {

void StringAppendV(std::string* dst, const char* format, va_list ap) {
// First try with a small fixed size buffer
static const int kSpaceLength = 1024;
char space[kSpaceLength];

// It's possible for methods that use a va_list to invalidate
// the data in it upon use. The fix is to make a copy
// of the structure before using it and use that copy instead.
va_list backup_ap;
va_copy(backup_ap, ap);
int result = vsnprintf(space, kSpaceLength, format, backup_ap);
va_end(backup_ap);

if (result < kSpaceLength) {
if (result >= 0) {
// Normal case -- everything fit.
dst->append(space, result);
return;
}

#ifdef _MSC_VER
// Error or MSVC running out of space. MSVC 8.0 and higher
// can be asked about space needed with the special idiom below:
va_copy(backup_ap, ap);
result = vsnprintf(nullptr, 0, format, backup_ap);
va_end(backup_ap);
#endif

if (result < 0) {
// Just an error.
return;
}
}

// Increase the buffer size to the size requested by vsnprintf,
// plus one for the closing \0.
size_t initial_size = dst->size();
size_t target_size = initial_size + result;

dst->resize(target_size + 1);
char* buf = &(*dst)[initial_size];
int buf_remain = result + 1;

// Restore the va_list before we use it again
va_copy(backup_ap, ap);
result = vsnprintf(buf, buf_remain, format, backup_ap);
va_end(backup_ap);

if (result >= 0 && result < buf_remain) {
// It fit and vsnprintf copied in directly. Resize down one to
// remove the trailing \0.
dst->resize(target_size);
} else {
// Didn't fit. Leave the original string unchanged.
dst->resize(initial_size);
}
}

std::string StringPrintf(const char* format, ...) {
va_list ap;
va_start(ap, format);
std::string result;
StringAppendV(&result, format, ap);
va_end(ap);
return result;
}

void StringAppendF(std::string* dst, const char* format, ...) {
va_list ap;
va_start(ap, format);
StringAppendV(dst, format, ap);
va_end(ap);
}

} // namespace util
} // namespace firestore
} // namespace firebase
48 changes: 48 additions & 0 deletions Firestore/core/src/firebase/firestore/util/string_printf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2018 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.
*/

#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_PRINTF_H_
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_PRINTF_H_

#include <stdarg.h>

#include <string>

#include <absl/base/attributes.h>

namespace firebase {
namespace firestore {
namespace util {

/** Return a C++ string. */
std::string StringPrintf(const char* format, ...)
ABSL_PRINTF_ATTRIBUTE(1, 2);

/** Append result to a supplied string. */
void StringAppendF(std::string* dst, const char* format, ...)
ABSL_PRINTF_ATTRIBUTE(2, 3);

/**
* Lower-level routine that takes a va_list and appends to a specified
* string. All other routines are just convenience wrappers around it.
*/
void StringAppendV(std::string* dst, const char* format, va_list ap);

} // namespace util
} // namespace firestore
} // namespace firebase

#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_FORMAT_H_
13 changes: 7 additions & 6 deletions Firestore/core/test/firebase/firestore/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cc_test(
firebase_firestore_util_test
autoid_test.cc
secure_random_test.cc
string_printf_test.cc
)
target_link_libraries(
firebase_firestore_util_test
Expand All @@ -24,20 +25,20 @@ target_link_libraries(

if(APPLE)
cc_test(
firebase_firestore_util_log_apple_test
firebase_firestore_util_apple_test
log_test.cc
)
target_link_libraries(
firebase_firestore_util_log_apple_test
firebase_firestore_util_log_apple
firebase_firestore_util_apple_test
firebase_firestore_util_apple
)
endif(APPLE)

cc_test(
firebase_firestore_util_log_stdio_test
firebase_firestore_util_stdio_test
log_test.cc
)
target_link_libraries(
firebase_firestore_util_log_stdio_test
firebase_firestore_util_log_stdio
firebase_firestore_util_stdio_test
firebase_firestore_util_stdio
)
Loading