66 | 66 |
#include "base/port.h"
|
67 | 67 |
#include "base/string_piece.h"
|
68 | 68 |
|
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 |
|
120 | 70 |
|
121 | 71 |
namespace mozc {
|
122 | 72 |
|
|
910 | 860 |
kUtf8MinGooglePuaEmoji <= s && s <= kUtf8MaxGooglePuaEmoji);
|
911 | 861 |
}
|
912 | 862 |
|
|
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 |
|
913 | 915 |
string Util::StringPrintf(const char *format, ...) {
|
914 | 916 |
va_list ap;
|
915 | 917 |
va_start(ap, format);
|
|
918 | 920 |
va_end(ap);
|
919 | 921 |
return result;
|
920 | 922 |
}
|
|
923 |
|
|
924 |
|
921 | 925 |
|
922 | 926 |
bool Util::ChopReturns(string *line) {
|
923 | 927 |
const string::size_type line_end = line->find_last_not_of("\r\n");
|