OLD | NEW |
1 /* | 1 /* |
2 * (C) 1999 Lars Knoll (knoll@kde.org) | 2 * (C) 1999 Lars Knoll (knoll@kde.org) |
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010, 2012 Apple Inc. All rights | 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010, 2012 Apple Inc. All rights |
4 * reserved. | 4 * reserved. |
5 * Copyright (C) 2007-2009 Torch Mobile, Inc. | 5 * Copyright (C) 2007-2009 Torch Mobile, Inc. |
6 * | 6 * |
7 * This library is free software; you can redistribute it and/or | 7 * This library is free software; you can redistribute it and/or |
8 * modify it under the terms of the GNU Library General Public | 8 * modify it under the terms of the GNU Library General Public |
9 * License as published by the Free Software Foundation; either | 9 * License as published by the Free Software Foundation; either |
10 * version 2 of the License, or (at your option) any later version. | 10 * version 2 of the License, or (at your option) any later version. |
11 * | 11 * |
12 * This library is distributed in the hope that it will be useful, | 12 * This library is distributed in the hope that it will be useful, |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 * Library General Public License for more details. | 15 * Library General Public License for more details. |
16 * | 16 * |
17 * You should have received a copy of the GNU Library General Public License | 17 * You should have received a copy of the GNU Library General Public License |
18 * along with this library; see the file COPYING.LIB. If not, write to | 18 * along with this library; see the file COPYING.LIB. If not, write to |
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 * Boston, MA 02110-1301, USA. | 20 * Boston, MA 02110-1301, USA. |
21 */ | 21 */ |
22 | 22 |
23 #include "wtf/text/WTFString.h" | 23 #include "wtf/text/WTFString.h" |
24 | 24 |
| 25 #include "base/strings/string_util.h" |
25 #include "wtf/ASCIICType.h" | 26 #include "wtf/ASCIICType.h" |
26 #include "wtf/DataLog.h" | 27 #include "wtf/DataLog.h" |
27 #include "wtf/HexNumber.h" | 28 #include "wtf/HexNumber.h" |
28 #include "wtf/MathExtras.h" | 29 #include "wtf/MathExtras.h" |
29 #include "wtf/StringExtras.h" | 30 #include "wtf/StringExtras.h" |
30 #include "wtf/Vector.h" | 31 #include "wtf/Vector.h" |
31 #include "wtf/dtoa.h" | 32 #include "wtf/dtoa.h" |
32 #include "wtf/text/CString.h" | 33 #include "wtf/text/CString.h" |
33 #include "wtf/text/CharacterNames.h" | 34 #include "wtf/text/CharacterNames.h" |
34 #include "wtf/text/IntegerToStringConversion.h" | 35 #include "wtf/text/IntegerToStringConversion.h" |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 } | 310 } |
310 | 311 |
311 String String::foldCase() const { | 312 String String::foldCase() const { |
312 if (!m_impl) | 313 if (!m_impl) |
313 return String(); | 314 return String(); |
314 return m_impl->foldCase(); | 315 return m_impl->foldCase(); |
315 } | 316 } |
316 | 317 |
317 String String::format(const char* format, ...) { | 318 String String::format(const char* format, ...) { |
318 va_list args; | 319 va_list args; |
| 320 |
| 321 // TODO(esprehn): base uses 1024, maybe we should use a bigger size too. |
| 322 static const unsigned kDefaultSize = 256; |
| 323 Vector<char, kDefaultSize> buffer(kDefaultSize); |
| 324 |
319 va_start(args, format); | 325 va_start(args, format); |
320 | 326 int length = base::vsnprintf(buffer.data(), buffer.size(), format, args); |
321 // Do the format once to get the length. | |
322 #if COMPILER(MSVC) | |
323 int result = _vscprintf(format, args); | |
324 #else | |
325 char ch; | |
326 int result = vsnprintf(&ch, 1, format, args); | |
327 // We need to call va_end() and then va_start() again here, as the | |
328 // contents of args is undefined after the call to vsnprintf | |
329 // according to http://man.cx/snprintf(3) | |
330 // | |
331 // Not calling va_end/va_start here happens to work on lots of | |
332 // systems, but fails e.g. on 64bit Linux. | |
333 #endif | |
334 va_end(args); | 327 va_end(args); |
335 | 328 |
336 if (result == 0) | 329 // TODO(esprehn): This can only happen if there's an encoding error, what's |
337 return String(""); | 330 // the locale set to inside blink? Can this happen? We should probably CHECK |
338 if (result < 0) | 331 // instead. |
| 332 if (length < 0) |
339 return String(); | 333 return String(); |
340 | 334 |
341 Vector<char, 256> buffer; | 335 if (static_cast<unsigned>(length) >= buffer.size()) { |
342 unsigned len = result; | 336 // vsnprintf doesn't include the NUL terminator in the length so we need to |
343 buffer.grow(len + 1); | 337 // add space for it when growing. |
| 338 buffer.grow(length + 1); |
344 | 339 |
345 va_start(args, format); | 340 // We need to call va_end() and then va_start() each time we use args, as |
346 // Now do the formatting again, guaranteed to fit. | 341 // the contents of args is undefined after the call to vsnprintf according |
347 vsnprintf(buffer.data(), buffer.size(), format, args); | 342 // to http://man.cx/snprintf(3) |
| 343 // |
| 344 // Not calling va_end/va_start here happens to work on lots of systems, but |
| 345 // fails e.g. on 64bit Linux. |
| 346 va_start(args, format); |
| 347 length = base::vsnprintf(buffer.data(), buffer.size(), format, args); |
| 348 va_end(args); |
| 349 } |
348 | 350 |
349 va_end(args); | 351 CHECK_LT(static_cast<unsigned>(length), buffer.size()); |
350 | 352 return String(reinterpret_cast<const LChar*>(buffer.data()), length); |
351 return StringImpl::create(reinterpret_cast<const LChar*>(buffer.data()), len); | |
352 } | 353 } |
353 | 354 |
354 template <typename IntegerType> | 355 template <typename IntegerType> |
355 static String integerToString(IntegerType input) { | 356 static String integerToString(IntegerType input) { |
356 IntegerToStringConverter<IntegerType> converter(input); | 357 IntegerToStringConverter<IntegerType> converter(input); |
357 return StringImpl::create(converter.characters8(), converter.length()); | 358 return StringImpl::create(converter.characters8(), converter.length()); |
358 } | 359 } |
359 | 360 |
360 String String::number(int number) { | 361 String String::number(int number) { |
361 return integerToString(number); | 362 return integerToString(number); |
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
846 } | 847 } |
847 buffer.append('\0'); | 848 buffer.append('\0'); |
848 return buffer; | 849 return buffer; |
849 } | 850 } |
850 | 851 |
851 Vector<char> asciiDebug(String& string) { | 852 Vector<char> asciiDebug(String& string) { |
852 return asciiDebug(string.impl()); | 853 return asciiDebug(string.impl()); |
853 } | 854 } |
854 | 855 |
855 #endif | 856 #endif |
OLD | NEW |