move Iterator, Enumerate, EvictingCacheMap, Foreach, Merge, and
[folly.git] / folly / detail / RangeCommon.cpp
1 /*
2  * Copyright 2017 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
17 #include <folly/detail/RangeCommon.h>
18
19 #include <bitset>
20
21 #include <folly/container/SparseByteSet.h>
22
23 namespace folly {
24
25 namespace detail {
26
27 size_t qfind_first_byte_of_bitset(
28     const StringPieceLite haystack,
29     const StringPieceLite needles) {
30   std::bitset<256> s;
31   for (auto needle : needles) {
32     s[(uint8_t)needle] = true;
33   }
34   for (size_t index = 0; index < haystack.size(); ++index) {
35     if (s[(uint8_t)haystack[index]]) {
36       return index;
37     }
38   }
39   return std::string::npos;
40 }
41
42 size_t qfind_first_byte_of_byteset(
43     const StringPieceLite haystack,
44     const StringPieceLite needles) {
45   SparseByteSet s;
46   for (auto needle : needles) {
47     s.add(uint8_t(needle));
48   }
49   for (size_t index = 0; index < haystack.size(); ++index) {
50     if (s.contains(uint8_t(haystack[index]))) {
51       return index;
52     }
53   }
54   return std::string::npos;
55 }
56 }
57 }