/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * 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 #include namespace folly { template class small_vector; template class fbvector; template class fbstring_core; template class basic_fbstring; namespace detail { using PredeclareFbString = basic_fbstring< char, std::char_traits, std::allocator, fbstring_core>; template struct SimdSplitByCharImpl { static void keepEmpty(char sep, folly::StringPiece what, Container& res); static void dropEmpty(char sep, folly::StringPiece what, Container& res); }; // Different name to easier identify in the stack potential performance issues template struct SimdSplitByCharImplToStrings { static void keepEmpty(char sep, folly::StringPiece what, Container& res); static void dropEmpty(char sep, folly::StringPiece what, Container& res); }; template constexpr bool isSimdSplitSupportedStringViewType = std::is_same::value || std::is_same::value; template constexpr bool isSimdSplitSupportedStringType = std::is_same::value || std::is_same::value; template struct SimdSplitByCharIsDefinedFor { static constexpr bool value = false; }; template struct SimdSplitByCharIsDefinedFor> { static constexpr bool value = isSimdSplitSupportedStringViewType || isSimdSplitSupportedStringType; }; template struct SimdSplitByCharIsDefinedFor> : SimdSplitByCharIsDefinedFor> {}; template struct SimdSplitByCharIsDefinedFor> { static constexpr bool value = isSimdSplitSupportedStringViewType && 0 < M && M <= 8; }; template std::enable_if_t< isSimdSplitSupportedStringViewType> simdSplitByChar( char sep, folly::StringPiece what, Container& res, bool ignoreEmpty) { static_assert( SimdSplitByCharIsDefinedFor::value, "simd split by char is supported only for vector/fbvector/small_vector, with small size <= 8." " The resulting string type has to string_view or StringPiece." " There is also a special case of (fb)vector<(fb)string> for legacy compatibility"); if (ignoreEmpty) { SimdSplitByCharImpl::dropEmpty(sep, what, res); } else { SimdSplitByCharImpl::keepEmpty(sep, what, res); } } template std::enable_if_t> simdSplitByChar( char sep, folly::StringPiece what, Container& res, bool ignoreEmpty) { static_assert( SimdSplitByCharIsDefinedFor::value, "simd split by char is supported only for vector/fbvector/small_vector, with small size <= 8." " The resulting string type has to string_view or StringPiece." " There is also a special case of (fb)vector<(fb)string> for legacy compatibility"); if (ignoreEmpty) { SimdSplitByCharImplToStrings::dropEmpty(sep, what, res); } else { SimdSplitByCharImplToStrings::keepEmpty(sep, what, res); } } // clang-format off #define FOLLY_DETAIL_DECLARE_ALL_SIMD_SPLIT_OVERLOADS(...) \ extern template struct SimdSplitByCharImpl>; \ extern template struct SimdSplitByCharImpl>>; \ extern template struct SimdSplitByCharImpl>; \ extern template struct SimdSplitByCharImpl>; \ extern template struct SimdSplitByCharImpl>; \ extern template struct SimdSplitByCharImpl>; \ extern template struct SimdSplitByCharImpl>; \ extern template struct SimdSplitByCharImpl>; \ extern template struct SimdSplitByCharImpl>; \ extern template struct SimdSplitByCharImpl>; // clang-format on FOLLY_DETAIL_DECLARE_ALL_SIMD_SPLIT_OVERLOADS(folly::StringPiece) FOLLY_DETAIL_DECLARE_ALL_SIMD_SPLIT_OVERLOADS(std::string_view) extern template struct SimdSplitByCharImplToStrings>; extern template struct SimdSplitByCharImplToStrings< std::vector>; extern template struct SimdSplitByCharImplToStrings< fbvector>>; extern template struct SimdSplitByCharImplToStrings< fbvector>>; #undef FOLLY_DETAIL_DECLARE_ALL_SIMD_SPLIT_OVERLOADS } // namespace detail } // namespace folly