summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorevanm@google.com <evanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-07 19:26:37 +0000
committerevanm@google.com <evanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-07 19:26:37 +0000
commita191e01106d91c8e0513f99dacdcfac8b9705c9f (patch)
tree0a859b029744618301bb5564dd18f63b259f2723
parent20da260f9f6bab65c196d6ad51320b313926aeba (diff)
downloadchromium_src-a191e01106d91c8e0513f99dacdcfac8b9705c9f.zip
chromium_src-a191e01106d91c8e0513f99dacdcfac8b9705c9f.tar.gz
chromium_src-a191e01106d91c8e0513f99dacdcfac8b9705c9f.tar.bz2
Rename and remove a bunch of string functions.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@527 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/string_util.cc16
-rw-r--r--base/string_util.h116
-rw-r--r--base/string_util_mac.cc41
-rw-r--r--base/string_util_posix.h (renamed from base/string_util_mac.h)28
-rw-r--r--base/string_util_win.h28
5 files changed, 62 insertions, 167 deletions
diff --git a/base/string_util.cc b/base/string_util.cc
index 4f095b7..5498652 100644
--- a/base/string_util.cc
+++ b/base/string_util.cc
@@ -637,7 +637,7 @@ bool StartsWithASCII(const std::string& str,
if (case_sensitive)
return str.compare(0, search.length(), search) == 0;
else
- return StrNCaseCmp(str.c_str(), search.c_str(), search.length()) == 0;
+ return base::strncasecmp(str.c_str(), search.c_str(), search.length()) == 0;
}
DataUnits GetByteDisplayUnits(int64 bytes) {
@@ -703,10 +703,12 @@ std::wstring FormatBytesInternal(int64 bytes,
double int_part;
double fractional_part = modf(unit_amount, &int_part);
modf(fractional_part * 10, &int_part);
- if (int_part == 0)
- SWPrintF(tmp, arraysize(tmp), L"%lld", static_cast<int64>(unit_amount));
- else
- SWPrintF(tmp, arraysize(tmp), L"%.1lf", unit_amount);
+ if (int_part == 0) {
+ base::swprintf(tmp, arraysize(tmp),
+ L"%lld", static_cast<int64>(unit_amount));
+ } else {
+ base::swprintf(tmp, arraysize(tmp), L"%.1lf", unit_amount);
+ }
std::wstring ret(tmp);
if (show_units) {
@@ -764,14 +766,14 @@ inline int vsnprintfT(char* buffer,
size_t buf_size,
const char* format,
va_list argptr) {
- return VSNPrintF(buffer, buf_size, format, argptr);
+ return base::vsnprintf(buffer, buf_size, format, argptr);
}
inline int vsnprintfT(wchar_t* buffer,
size_t buf_size,
const wchar_t* format,
va_list argptr) {
- return VSWPrintF(buffer, buf_size, format, argptr);
+ return base::vswprintf(buffer, buf_size, format, argptr);
}
// Templatized backend for StringPrintF/StringAppendF. This does not finalize
diff --git a/base/string_util.h b/base/string_util.h
index 4e52daf..1a4080a 100644
--- a/base/string_util.h
+++ b/base/string_util.h
@@ -34,72 +34,69 @@
#include <string>
#include <vector>
+#include <stdarg.h> // va_list
#include "base/basictypes.h"
-// Safe standard library wrappers for all platforms. The Str* variants
-// operate on NUL-terminated char* strings, like the standard library's str*
-// functions.
+// Safe standard library wrappers for all platforms.
-// Copy at most (dst_size - 1) characters from src to dest, guaranteeing dst
-// will be NUL-terminated. If the string is copied without truncation,
-// returns true. dst is undefined if the string cannot be copied without
-// truncation, and the function will either return false or cause termination.
-bool StrCpy(char* dest, const char* src, size_t dst_size);
+namespace base {
-// As with StrCpy, but copies at most the minimum of (dst_size - 1) and
-// src_size characters.
-bool StrNCpy(char* dest, const char* src, size_t dst_size, size_t src_size);
+// C standard-library functions like "strncasecmp" and "snprintf" that aren't
+// cross-platform are provided as "base::strncasecmp", and their prototypes
+// are listed below. These functions are then implemented as inline calls
+// to the platform-specific equivalents in the platform-specific headers.
// Compare up to count characters of s1 and s2 without regard to case using
// the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if
// s2 > s1 according to a lexicographic comparison.
-int StrNCaseCmp(const char* s1, const char* s2, size_t count);
+int strncasecmp(const char* s1, const char* s2, size_t count);
-// Wrapper for vsnprintf, snprintf that always NUL-terminates and always
-// returns the number of characters that would be in an untruncated formatted
+// Wrapper for vsnprintf that always NUL-terminates and always returns the
+// number of characters that would be in an untruncated formatted
// string, even when truncation occurs.
-int VSNPrintF(char* buffer, size_t size,
- const char* format, va_list arguments);
-int SNPrintF(char* buffer, size_t size, const char* format, ...);
+int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments);
-// The Wcs* variants operate on NUL-terminated wchar_t* strings, like the
-// standard library's wcs* functions. Otherwise, these behave the same as
-// the Str* variants above.
-
-bool WcsCpy(wchar_t* dest, const wchar_t* src, size_t dst_size);
-bool WcsNCpy(wchar_t* dest, const wchar_t* src, size_t dst_size);
-
-int VSWPrintF(wchar_t* buffer, size_t size,
+int vswprintf(wchar_t* buffer, size_t size,
const wchar_t* format, va_list arguments);
-int SWPrintF(wchar_t* buffer, size_t size, const wchar_t* format, ...);
// Some of these implementations need to be inlined.
-#if defined(OS_WIN)
-#include "base/string_util_win.h"
-#elif defined(OS_MACOSX)
-#include "base/string_util_mac.h"
-#else
-#error Define string operations appropriately for your platform
-#endif
-
-inline int SNPrintF(char* buffer, size_t size, const char* format, ...) {
+inline int snprintf(char* buffer, size_t size, const char* format, ...) {
va_list arguments;
va_start(arguments, format);
- int result = VSNPrintF(buffer, size, format, arguments);
+ int result = vsnprintf(buffer, size, format, arguments);
va_end(arguments);
return result;
}
-inline int SWPrintF(wchar_t* buffer, size_t size, const wchar_t* format, ...) {
+inline int swprintf(wchar_t* buffer, size_t size, const wchar_t* format, ...) {
va_list arguments;
va_start(arguments, format);
- int result = VSWPrintF(buffer, size, format, arguments);
+ int result = vswprintf(buffer, size, format, arguments);
va_end(arguments);
return result;
}
+} // namespace base
+
+#if defined(OS_WIN)
+#include "base/string_util_win.h"
+#elif defined(OS_POSIX)
+#include "base/string_util_posix.h"
+#else
+#error Define string operations appropriately for your platform
+#endif
+
+// Old names for the above string functions, kept for compatibility.
+// TODO(evanm): excise all references to these old names.
+#define StrNCaseCmp base::strncasecmp
+#define SWPrintF base::swprintf
+#define VSNPrintF base::vsnprintf
+#define SNPrintF base::snprintf
+#define SWPrintF base::swprintf
+
+
// Returns a reference to a globally unique empty string that functions can
// return. Use this to avoid static construction of strings, not to replace
// any and all uses of "std::string()" as nicer-looking sugar.
@@ -406,49 +403,6 @@ inline char_type* WriteInto(
}
//-----------------------------------------------------------------------------
-// CharTraits is provides wrappers with common function names for char/wchar_t
-// specific CRT functions.
-
-template <class CharT> struct CharTraits {
-};
-
-template <>
-struct CharTraits<char> {
- static inline size_t length(const char* s) {
- return strlen(s);
- }
- static inline bool copy(char* dst, size_t dst_size, const char* s) {
- return StrCpy(dst, s, dst_size);
- }
- static inline bool copy_num(char* dst, size_t dst_size, const char* s,
- size_t s_len) {
- if (dst_size < (s_len + 1))
- return false;
- memcpy(dst, s, s_len);
- dst[s_len] = '\0';
- return true;
- }
-};
-
-template <>
-struct CharTraits<wchar_t> {
- static inline size_t length(const wchar_t* s) {
- return wcslen(s);
- }
- static inline bool copy(wchar_t* dst, size_t dst_size, const wchar_t* s) {
- return WcsCpy(dst, s, dst_size);
- }
- static inline bool copy_num(wchar_t* dst, size_t dst_size, const wchar_t* s,
- size_t s_len) {
- if (dst_size < (s_len + 1))
- return false;
- memcpy(dst, s, s_len * sizeof(wchar_t));
- dst[s_len] = '\0';
- return true;
- }
-};
-
-//-----------------------------------------------------------------------------
// Function objects to aid in comparing/searching strings.
diff --git a/base/string_util_mac.cc b/base/string_util_mac.cc
index 614bbcc..310f93b 100644
--- a/base/string_util_mac.cc
+++ b/base/string_util_mac.cc
@@ -37,47 +37,6 @@
#include "base/scoped_cftyperef.h"
#include "unicode/numfmt.h"
-// Can't use strlcpy/wcslcpy, because they always returns the length of src,
-// making it impossible to detect overflow. Because the reimplementation is
-// too large to inline, StrNCpy and WcsNCpy are in this file, but since they
-// don't make any non-inlined calls, there's no penalty relative to the libc
-// routines.
-template<typename CharType>
-static inline bool StrNCpyT(CharType* dst, const CharType* src,
- size_t dst_size, size_t src_size) {
- // The initial value of count has room for a NUL terminator.
- size_t count = std::min(dst_size, src_size + 1);
- if (count == 0)
- return false;
-
- // Copy up to (count - 1) bytes, or until reaching a NUL terminator
- while (--count != 0) {
- if ((*dst++ = *src++) == '\0')
- break;
- }
-
- // If the break never occurred, append a NUL terminator
- if (count == 0) {
- *dst = '\0';
-
- // If the string was truncated, return false
- if (*src != '\0')
- return false;
- }
-
- return true;
-}
-
-bool StrNCpy(char* dst, const char* src,
- size_t dst_size, size_t src_size) {
- return StrNCpyT(dst, src, dst_size, src_size);
-}
-
-bool WcsNCpy(wchar_t* dst, const wchar_t* src,
- size_t dst_size, size_t src_size) {
- return StrNCpyT(dst, src, dst_size, src_size);
-}
-
static NumberFormat* number_format_singleton = NULL;
static CFDateFormatterRef date_formatter = NULL;
static CFDateFormatterRef time_formatter = NULL;
diff --git a/base/string_util_mac.h b/base/string_util_posix.h
index 26201b7..b3df2ad 100644
--- a/base/string_util_mac.h
+++ b/base/string_util_posix.h
@@ -27,36 +27,30 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#ifndef BASE_STRING_UTIL_MAC_H__
-#define BASE_STRING_UTIL_MAC_H__
+#ifndef BASE_STRING_UTIL_POSIX_H_
+#define BASE_STRING_UTIL_POSIX_H_
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
-inline bool StrCpy(char* dst, const char* src, size_t dst_size) {
- return strlcpy(dst, src, dst_size) < dst_size;
-}
+namespace base {
-inline int StrNCaseCmp(const char* string1, const char* string2, size_t count) {
- return strncasecmp(string1, string2, count);
+inline int strncasecmp(const char* string1, const char* string2, size_t count) {
+ return ::strncasecmp(string1, string2, count);
}
-inline int VSNPrintF(char* buffer, size_t size,
+inline int vsnprintf(char* buffer, size_t size,
const char* format, va_list arguments) {
- return vsnprintf(buffer, size, format, arguments);
-}
-
-inline bool WcsCpy(char* dst, const char* src, size_t dst_size) {
- return strlcpy(dst, src, dst_size) < dst_size;
+ return ::vsnprintf(buffer, size, format, arguments);
}
-inline int VSWPrintF(wchar_t* buffer, size_t size,
+inline int vswprintf(wchar_t* buffer, size_t size,
const wchar_t* format, va_list arguments) {
- return vswprintf(buffer, size, format, arguments);
+ return ::vswprintf(buffer, size, format, arguments);
}
-// StrNCpy and WcsNCpy are not inline, so they're implemented in the .cc file.
+} // namespace base
-#endif // BASE_STRING_UTIL_MAC_H__
+#endif // BASE_STRING_UTIL_POSIX_H_
diff --git a/base/string_util_win.h b/base/string_util_win.h
index 151a428..bec84042 100644
--- a/base/string_util_win.h
+++ b/base/string_util_win.h
@@ -35,37 +35,21 @@
#include <string.h>
#include <wchar.h>
-inline bool StrCpy(char* dst, const char* src, size_t dst_size) {
- return strcpy_s(dst, dst_size, src) == 0;
-}
-
-inline bool StrNCpy(char* dst, const char* src,
- size_t dst_size, size_t src_size) {
- return strncpy_s(dst, dst_size, src, src_size) == 0;
-}
+namespace base {
-inline int StrNCaseCmp(const char* s1, const char* s2, size_t count) {
+inline int strncasecmp(const char* s1, const char* s2, size_t count) {
return _strnicmp(s1, s2, count);
}
-inline int VSNPrintF(char* buffer, size_t size,
- const char* format, va_list arguments) {
+inline int vsnprintf(char* buffer, size_t size,
+ const char* format, va_list arguments) {
int length = vsnprintf_s(buffer, size, size - 1, format, arguments);
if (length < 0)
return _vscprintf(format, arguments);
return length;
}
-inline bool WcsCpy(wchar_t* dst, const wchar_t* src, size_t dst_size) {
- return wcscpy_s(dst, dst_size, src) == 0;
-}
-
-inline bool WcsNCpy(wchar_t* dst, const wchar_t* src,
- size_t dst_size, size_t src_size) {
- return wcsncpy_s(dst, dst_size, src, src_size) == 0;
-}
-
-inline int VSWPrintF(wchar_t* buffer, size_t size,
+inline int vswprintf(wchar_t* buffer, size_t size,
const wchar_t* format, va_list arguments) {
int length = _vsnwprintf_s(buffer, size, size - 1, format, arguments);
if (length < 0)
@@ -73,4 +57,6 @@ inline int VSWPrintF(wchar_t* buffer, size_t size,
return length;
}
+} // namespace base
+
#endif // BASE_STRING_UTIL_WIN_H__