From 8ff7c8b8270a20fd768b57ea6c1b6dd48d849e9f Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Mon, 28 Mar 2016 11:22:53 -0700 Subject: [PATCH] Create the string.h portability header Summary: Windows has it, but some things aren't there, and some have different names. Reviewed By: yfeldblum Differential Revision: D2990475 fb-gh-sync-id: 3f30e084eb7789c13b3981ec0fbf5b7b642c3367 fbshipit-source-id: 3f30e084eb7789c13b3981ec0fbf5b7b642c3367 --- folly/Makefile.am | 2 ++ folly/Range.h | 7 ++---- folly/portability/String.cpp | 49 ++++++++++++++++++++++++++++++++++++ folly/portability/String.h | 33 ++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 5 deletions(-) create mode 100755 folly/portability/String.cpp create mode 100755 folly/portability/String.h diff --git a/folly/Makefile.am b/folly/Makefile.am index 0f8915a1..e421304b 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -274,6 +274,7 @@ nobase_follyinclude_HEADERS = \ portability/GFlags.h \ portability/IOVec.h \ portability/Memory.h \ + portability/String.h \ portability/Strings.h \ portability/Syscall.h \ portability/SysFile.h \ @@ -413,6 +414,7 @@ libfolly_la_SOURCES = \ MacAddress.cpp \ MemoryMapping.cpp \ portability/Environment.cpp \ + portability/String.cpp \ portability/Strings.cpp \ portability/SysFile.cpp \ portability/SysMman.cpp \ diff --git a/folly/Range.h b/folly/Range.h index d42fe548..4913aef4 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -20,10 +20,11 @@ #ifndef FOLLY_RANGE_H_ #define FOLLY_RANGE_H_ -#include #include +#include #include #include +#include #include #include @@ -1085,14 +1086,12 @@ inline size_t qfind(const Range& haystack, const char& needle) { return pos == nullptr ? std::string::npos : pos - haystack.data(); } -#if FOLLY_HAVE_MEMRCHR template <> inline size_t rfind(const Range& haystack, const char& needle) { auto pos = static_cast( ::memrchr(haystack.data(), needle, haystack.size())); return pos == nullptr ? std::string::npos : pos - haystack.data(); } -#endif // specialization for ByteRange template <> @@ -1103,7 +1102,6 @@ inline size_t qfind(const Range& haystack, return pos == nullptr ? std::string::npos : pos - haystack.data(); } -#if FOLLY_HAVE_MEMRCHR template <> inline size_t rfind(const Range& haystack, const unsigned char& needle) { @@ -1111,7 +1109,6 @@ inline size_t rfind(const Range& haystack, ::memrchr(haystack.data(), needle, haystack.size())); return pos == nullptr ? std::string::npos : pos - haystack.data(); } -#endif template size_t qfind_first_of(const Range& haystack, diff --git a/folly/portability/String.cpp b/folly/portability/String.cpp new file mode 100755 index 00000000..eae93f5e --- /dev/null +++ b/folly/portability/String.cpp @@ -0,0 +1,49 @@ +/* + * Copyright 2016 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#if !FOLLY_HAVE_MEMRCHR || defined(_WIN32) || defined(__APPLE__) +extern "C" void* memrchr(const void* s, int c, size_t n) { + for (auto p = ((const char*)s) + n - 1; p >= (const char*)s; p--) { + if (*p == (char)c) { + return (void*)p; + } + } + return nullptr; +} +#endif + +#ifdef _WIN32 +#include + +extern "C" { +char* strndup(const char* a, size_t len) { + auto neededLen = strlen(a); + if (neededLen > len) { + neededLen = len - 1; + } + char* buf = (char*)malloc((neededLen + 1) * sizeof(char)); + memcpy(buf, a, neededLen); + buf[neededLen] = '\0'; + return buf; +} + +char* strtok_r(char* str, char const* delim, char** ctx) { + return strtok_s(str, delim, ctx); +} +} +#endif diff --git a/folly/portability/String.h b/folly/portability/String.h new file mode 100755 index 00000000..5dd8a654 --- /dev/null +++ b/folly/portability/String.h @@ -0,0 +1,33 @@ +/* + * Copyright 2016 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +#include + +#if !FOLLY_HAVE_MEMRCHR || defined(_WIN32) || defined(__APPLE__) +#include +extern "C" void* memrchr(const void* s, int c, size_t n); +#endif + +#ifdef _WIN32 +extern "C" { +char* strndup(const char* a, size_t len); +char* strtok_r(char* str, char const* delim, char** ctx); +} +#endif -- 2.34.1