1 // Copyright 2014 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "include/string_util.h"
6
7 #include <errno.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <strings.h>
11
12 #include "include/macros.h"
13
14 namespace gestures {
15
16 namespace {
17
18 const char kWhitespaceASCII[] = {
19 0x09, // CHARACTER TABULATION
20 0x0A, // LINE FEED (LF)
21 0x0B, // LINE TABULATION
22 0x0C, // FORM FEED (FF)
23 0x0D, // CARRIAGE RETURN (CR)
24 0x20, // SPACE
25 0
26 };
27
28 // Backend for StringPrintF/StringAppendF. This does not finalize
29 // the va_list, the caller is expected to do that.
30 PRINTF_FORMAT(2, 0)
StringAppendVT(std::string * dst,const char * format,va_list ap)31 static void StringAppendVT(std::string* dst,
32 const char* format,
33 va_list ap) {
34 // First try with a small fixed size buffer.
35 // This buffer size should be kept in sync with StringUtilTest.GrowBoundary
36 // and StringUtilTest.StringPrintfBounds.
37 char stack_buf[1024];
38
39 va_list ap_copy;
40 va_copy(ap_copy, ap);
41
42 errno = 0;
43 int result = vsnprintf(stack_buf, arraysize(stack_buf), format, ap_copy);
44 va_end(ap_copy);
45
46 if (result >= 0 && result < static_cast<int>(arraysize(stack_buf))) {
47 // It fit.
48 dst->append(stack_buf, result);
49 return;
50 }
51
52 // Repeatedly increase buffer size until it fits.
53 int mem_length = arraysize(stack_buf);
54 while (true) {
55 if (result < 0) {
56 if (errno != 0 && errno != EOVERFLOW)
57 return;
58 // Try doubling the buffer size.
59 mem_length *= 2;
60 } else {
61 // We need exactly "result + 1" characters.
62 mem_length = result + 1;
63 }
64
65 if (mem_length > 32 * 1024 * 1024) {
66 // That should be plenty, don't try anything larger. This protects
67 // against huge allocations when using vsnprintfT implementations that
68 // return -1 for reasons other than overflow without setting errno.
69 return;
70 }
71
72 std::vector<char> mem_buf(mem_length);
73
74 // NOTE: You can only use a va_list once. Since we're in a while loop, we
75 // need to make a new copy each time so we don't use up the original.
76 va_copy(ap_copy, ap);
77 result = vsnprintf(&mem_buf[0], mem_length, format, ap_copy);
78 va_end(ap_copy);
79
80 if ((result >= 0) && (result < mem_length)) {
81 // It fit.
82 dst->append(&mem_buf[0], result);
83 return;
84 }
85 }
86 }
87
88 template<typename STR>
TrimStringT(const STR & input,const typename STR::value_type trim_chars[])89 STR TrimStringT(const STR& input, const typename STR::value_type trim_chars[]) {
90 if (input.empty()) {
91 return "";
92 }
93
94 // Find the edges of leading/trailing whitespace.
95 const typename STR::size_type first_good_char =
96 input.find_first_not_of(trim_chars);
97 const typename STR::size_type last_good_char =
98 input.find_last_not_of(trim_chars);
99
100 if (first_good_char == STR::npos || last_good_char == STR::npos) {
101 return "";
102 }
103
104 // Trim the whitespace.
105 return input.substr(first_good_char, last_good_char - first_good_char + 1);
106 }
107
108 } // namespace
109
StringAppendV(std::string * dst,const char * format,va_list ap)110 void StringAppendV(std::string* dst, const char* format, va_list ap) {
111 StringAppendVT(dst, format, ap);
112 }
113
StringPrintf(const char * format,...)114 std::string StringPrintf(const char* format, ...) {
115 va_list ap;
116 va_start(ap, format);
117 std::string result;
118 StringAppendV(&result, format, ap);
119 va_end(ap);
120 return result;
121 }
122
TrimWhitespaceASCII(const std::string & input)123 std::string TrimWhitespaceASCII(const std::string& input) {
124 return TrimStringT(input, kWhitespaceASCII);
125 }
126
127 } // namespace gestures
128