2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <glog/logging.h>
24 #include <folly/Likely.h>
31 * The qfind_first_byte_of_* functions are declared here, before Range.h, so
32 * they cannot take StringPiece values. But they're there to operate on
33 * StringPiece values. Dependency cycles: fun.
35 * StringPieceLite is here to break that dependency cycle.
37 class StringPieceLite {
39 StringPieceLite(const char* b, const char* e) : b_(b), e_(e) {}
40 template <typename Range>
41 /* implicit */ StringPieceLite(const Range& r)
42 : StringPieceLite(r.data(), r.data() + r.size()) {}
43 const char* data() const {
46 const char* begin() const {
49 const char* end() const {
53 return size_t(e_ - b_);
58 const char& operator[](size_t i) const {
62 template <typename Range>
63 explicit operator Range() const {
64 return Range(begin(), end());
72 inline size_t qfind_first_byte_of_std(
73 const StringPieceLite haystack,
74 const StringPieceLite needles) {
75 auto ret = std::find_first_of(
80 [](char a, char b) { return a == b; });
81 return ret == haystack.end() ? std::string::npos : ret - haystack.begin();
84 size_t qfind_first_byte_of_bitset(
85 const StringPieceLite haystack,
86 const StringPieceLite needles);
88 size_t qfind_first_byte_of_byteset(
89 const StringPieceLite haystack,
90 const StringPieceLite needles);
92 inline size_t qfind_first_byte_of_nosse(
93 const StringPieceLite haystack,
94 const StringPieceLite needles) {
95 if (UNLIKELY(needles.empty() || haystack.empty())) {
96 return std::string::npos;
98 // The thresholds below were empirically determined by benchmarking.
99 // This is not an exact science since it depends on the CPU, the size of
100 // needles, and the size of haystack.
101 if ((needles.size() >= 4 && haystack.size() <= 10) ||
102 (needles.size() >= 16 && haystack.size() <= 64) || needles.size() >= 32) {
103 return qfind_first_byte_of_byteset(haystack, needles);
105 return qfind_first_byte_of_std(haystack, needles);