X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/b37bf2e156556c589aea3e1f58a377f2b1189665..14957cd040308e3eeec43d26bae5d76da13fcd85:/wtf/StringExtras.h?ds=inline diff --git a/wtf/StringExtras.h b/wtf/StringExtras.h index ca1c20c..371e33b 100644 --- a/wtf/StringExtras.h +++ b/wtf/StringExtras.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2006, 2010 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -26,27 +26,100 @@ #ifndef WTF_StringExtras_h #define WTF_StringExtras_h -#include #include +#include +#include + +#if HAVE(STRINGS_H) +#include +#endif #if COMPILER(MSVC) +// FIXME: why a COMPILER check instead of OS? also, these should be HAVE checks -inline int snprintf(char *str, size_t size, const char* format, ...) +inline int snprintf(char* buffer, size_t count, const char* format, ...) { int result; va_list args; va_start(args, format); - result = _vsnprintf(str, size, format, args); + result = _vsnprintf(buffer, count, format, args); va_end(args); + + // In the case where the string entirely filled the buffer, _vsnprintf will not + // null-terminate it, but snprintf must. + if (count > 0) + buffer[count - 1] = '\0'; + return result; } -#if COMPILER(MSVC7) -// MSVC8 and above define this function -#define vsnprintf snprintf +inline double wtf_vsnprintf(char* buffer, size_t count, const char* format, va_list args) +{ + int result = _vsnprintf(buffer, count, format, args); + + // In the case where the string entirely filled the buffer, _vsnprintf will not + // null-terminate it, but vsnprintf must. + if (count > 0) + buffer[count - 1] = '\0'; + + return result; +} + +// Work around a difference in Microsoft's implementation of vsnprintf, where +// vsnprintf does not null terminate the buffer. WebKit can rely on the null termination. +#define vsnprintf(buffer, count, format, args) wtf_vsnprintf(buffer, count, format, args) + +#if OS(WINCE) + +inline int strnicmp(const char* string1, const char* string2, size_t count) +{ + return _strnicmp(string1, string2, count); +} + +inline int stricmp(const char* string1, const char* string2) +{ + return _stricmp(string1, string2); +} + +inline char* strdup(const char* strSource) +{ + return _strdup(strSource); +} + +#endif + +inline int strncasecmp(const char* s1, const char* s2, size_t len) +{ + return _strnicmp(s1, s2, len); +} + +inline int strcasecmp(const char* s1, const char* s2) +{ + return _stricmp(s1, s2); +} + #endif -inline int strncasecmp(const char* s1, const char* s2, size_t len) { return strnicmp(s1, s2, len); } +#if !HAVE(STRNSTR) + +inline char* strnstr(const char* buffer, const char* target, size_t bufferLength) +{ + size_t targetLength = strlen(target); + if (targetLength == 0) + return const_cast(buffer); + for (const char* start = buffer; *start && start + targetLength <= buffer + bufferLength; start++) { + if (*start == *target && strncmp(start + 1, target + 1, targetLength - 1) == 0) + return const_cast(start); + } + return 0; +} + +#endif + +#if COMPILER(RVCT) && __ARMCC_VERSION < 400000 + +int strcasecmp(const char* s1, const char* s2); +int strncasecmp(const char* s1, const char* s2, size_t len); #endif