Codebase list mozc / e07aa61
Code clean up for Util::StringPrintf REF_BUG=73356467 REF_CL=186177667,186249439 REF_TIME=2018-02-19T13:25:48+09:00 REF_TIME_RAW=1519014348 +0900 Hiroyuki Komatsu 6 years ago
4 changed file(s) with 63 addition(s) and 57 deletion(s). Raw diff Collapse all Expand all
6666 #include "base/port.h"
6767 #include "base/string_piece.h"
6868
69 namespace {
70
71 // Lower-level routine that takes a va_list and appends to a specified
72 // string. All other routines of sprintf family are just convenience
73 // wrappers around it.
74 void StringAppendV(string *dst, const char *format, va_list ap) {
75 // First try with a small fixed size buffer
76 char space[1024];
77
78 // It's possible for methods that use a va_list to invalidate
79 // the data in it upon use. The fix is to make a copy
80 // of the structure before using it and use that copy instead.
81 va_list backup_ap;
82 va_copy(backup_ap, ap);
83 int result = vsnprintf(space, sizeof(space), format, backup_ap);
84 va_end(backup_ap);
85
86 if ((result >= 0) && (result < sizeof(space))) {
87 // It fit
88 dst->append(space, result);
89 return;
90 }
91
92 // Repeatedly increase buffer size until it fits
93 int length = sizeof(space);
94 while (true) {
95 if (result < 0) {
96 // Older behavior: just try doubling the buffer size
97 length *= 2;
98 } else {
99 // We need exactly "result+1" characters
100 length = result+1;
101 }
102 char *buf = new char[length];
103
104 // Restore the va_list before we use it again
105 va_copy(backup_ap, ap);
106 result = vsnprintf(buf, length, format, backup_ap);
107 va_end(backup_ap);
108
109 if ((result >= 0) && (result < length)) {
110 // It fit
111 dst->append(buf, result);
112 delete[] buf;
113 return;
114 }
115 delete[] buf;
116 }
117 }
118
119 } // namespace
69
12070
12171 namespace mozc {
12272
910860 kUtf8MinGooglePuaEmoji <= s && s <= kUtf8MaxGooglePuaEmoji);
911861 }
912862
863 namespace {
864
865 // Lower-level routine that takes a va_list and appends to a specified
866 // string. All other routines of sprintf family are just convenience
867 // wrappers around it.
868 void StringAppendV(string *dst, const char *format, va_list ap) {
869 // First try with a small fixed size buffer
870 char space[1024];
871
872 // It's possible for methods that use a va_list to invalidate
873 // the data in it upon use. The fix is to make a copy
874 // of the structure before using it and use that copy instead.
875 va_list backup_ap;
876 va_copy(backup_ap, ap);
877 int result = vsnprintf(space, sizeof(space), format, backup_ap);
878 va_end(backup_ap);
879
880 if ((result >= 0) && (result < sizeof(space))) {
881 // It fit
882 dst->append(space, result);
883 return;
884 }
885
886 // Repeatedly increase buffer size until it fits
887 int length = sizeof(space);
888 while (true) {
889 if (result < 0) {
890 // Older behavior: just try doubling the buffer size
891 length *= 2;
892 } else {
893 // We need exactly "result+1" characters
894 length = result+1;
895 }
896 char *buf = new char[length];
897
898 // Restore the va_list before we use it again
899 va_copy(backup_ap, ap);
900 result = vsnprintf(buf, length, format, backup_ap);
901 va_end(backup_ap);
902
903 if ((result >= 0) && (result < length)) {
904 // It fit
905 dst->append(buf, result);
906 delete[] buf;
907 return;
908 }
909 delete[] buf;
910 }
911 }
912
913 } // namespace
914
913915 string Util::StringPrintf(const char *format, ...) {
914916 va_list ap;
915917 va_start(ap, format);
918920 va_end(ap);
919921 return result;
920922 }
923
924
921925
922926 bool Util::ChopReturns(string *line) {
923927 const string::size_type line_end = line->find_last_not_of("\r\n");
3838 #include "base/double_array.h"
3939 #include "base/port.h"
4040 #include "base/string_piece.h"
41
4142
4243 namespace mozc {
4344
268269 // in the range of Android Emoji PUA.
269270 static bool IsAndroidPuaEmoji(StringPiece s);
270271
272
271273 // C++ string version of sprintf.
272274 static string StringPrintf(const char *format, ...)
273275 // Tell the compiler to do printf format string checking.
274276 ABSL_PRINTF_ATTRIBUTE(1, 2);
277
275278
276279 // Chop the return characters (i.e. '\n' and '\r') at the end of the
277280 // given line.
2929
3030 MAJOR=2
3131 MINOR=23
32 BUILD=2808
32 BUILD=2809
3333 REVISION=102
3434 # This version represents the version of Mozc IME engine (converter, predictor,
3535 # etc.). This version info is included both in the Mozc server and in the Mozc
6767 TEST(GenericLruStorageTest, BasicOperations) {
6868 const int kValueSize = 12;
6969 const int kSize = 10;
70 const string kPrinfFormat = Util::StringPrintf("%%%dlu", kValueSize);
70 constexpr const char *kPrintFormat = "%12zu"; // 12 = kValueSize
7171
7272 GenericLruStorage storage(
7373 GetTemporaryFilePath().data(), kValueSize, kSize, 123);
7474 // Inserts (kSize + 1) entries.
7575 for (size_t i = 0; i < kSize + 1; ++i) {
76 const string value = Util::StringPrintf(kPrinfFormat.data(), i);
76 const string value = Util::StringPrintf(kPrintFormat, i);
7777 const string key = string("key") + value;
7878 storage.Insert(key, value.data());
7979 // Check the existence.
8383 // First inserted entry is already pushed out.
8484 EXPECT_EQ(NULL, storage.Lookup("0"));
8585 for (size_t i = 1; i < kSize + 1; ++i) {
86 const string value = Util::StringPrintf(kPrinfFormat.data(), i);
86 const string value = Util::StringPrintf(kPrintFormat, i);
8787 const string key = string("key") + value;
8888 EXPECT_EQ(value, string(storage.Lookup(key), kValueSize));
8989 }
9393 storage.GetAllValues(&values);
9494 EXPECT_EQ(kSize, values.size());
9595 for (size_t i = 1; i < kSize + 1; ++i) {
96 EXPECT_EQ(Util::StringPrintf(kPrinfFormat.data(), i),
97 values.at(kSize - i));
96 EXPECT_EQ(Util::StringPrintf(kPrintFormat, i), values.at(kSize - i));
9897 }
9998
10099 // Clean