RangeCommon.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright 2015-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/detail/RangeCommon.h>
  17. #include <bitset>
  18. #include <folly/container/SparseByteSet.h>
  19. namespace folly {
  20. namespace detail {
  21. size_t qfind_first_byte_of_bitset(
  22. const StringPieceLite haystack,
  23. const StringPieceLite needles) {
  24. std::bitset<256> s;
  25. for (auto needle : needles) {
  26. s[(uint8_t)needle] = true;
  27. }
  28. for (size_t index = 0; index < haystack.size(); ++index) {
  29. if (s[(uint8_t)haystack[index]]) {
  30. return index;
  31. }
  32. }
  33. return std::string::npos;
  34. }
  35. size_t qfind_first_byte_of_byteset(
  36. const StringPieceLite haystack,
  37. const StringPieceLite needles) {
  38. SparseByteSet s;
  39. for (auto needle : needles) {
  40. s.add(uint8_t(needle));
  41. }
  42. for (size_t index = 0; index < haystack.size(); ++index) {
  43. if (s.contains(uint8_t(haystack[index]))) {
  44. return index;
  45. }
  46. }
  47. return std::string::npos;
  48. }
  49. } // namespace detail
  50. } // namespace folly