X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/ba379fdc102753d6be2c4d937058fe40257329fe..14957cd040308e3eeec43d26bae5d76da13fcd85:/wtf/StringExtras.h diff --git a/wtf/StringExtras.h b/wtf/StringExtras.h index 1c23390..371e33b 100644 --- a/wtf/StringExtras.h +++ b/wtf/StringExtras.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006 Apple 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 @@ -28,12 +28,14 @@ #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* buffer, size_t count, const char* format, ...) { @@ -42,19 +44,32 @@ inline int snprintf(char* buffer, size_t count, const char* format, ...) va_start(args, format); 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) || PLATFORM(WINCE) - -inline int vsnprintf(char* buffer, size_t count, const char* format, va_list args) +inline double wtf_vsnprintf(char* buffer, size_t count, const char* format, va_list args) { - return _vsnprintf(buffer, count, format, 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; } -#endif +// 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 PLATFORM(WINCE) +#if OS(WINCE) inline int strnicmp(const char* string1, const char* string2, size_t count) { @@ -75,14 +90,37 @@ inline char* strdup(const char* strSource) inline int strncasecmp(const char* s1, const char* s2, size_t len) { - return strnicmp(s1, s2, len); + return _strnicmp(s1, s2, len); } inline int strcasecmp(const char* s1, const char* s2) { - return stricmp(s1, s2); + return _stricmp(s1, s2); } #endif +#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 + #endif // WTF_StringExtras_h