Range.h 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. /*
  2. * Copyright 2011-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. // @author Mark Rabkin (mrabkin@fb.com)
  17. // @author Andrei Alexandrescu (andrei.alexandrescu@fb.com)
  18. #pragma once
  19. #include <folly/Portability.h>
  20. #include <folly/hash/SpookyHashV2.h>
  21. #include <folly/lang/Exception.h>
  22. #include <folly/portability/Constexpr.h>
  23. #include <folly/portability/String.h>
  24. #include <algorithm>
  25. #include <array>
  26. #include <cassert>
  27. #include <climits>
  28. #include <cstddef>
  29. #include <cstring>
  30. #include <iosfwd>
  31. #include <iterator>
  32. #include <stdexcept>
  33. #include <string>
  34. #include <type_traits>
  35. #if FOLLY_HAS_STRING_VIEW
  36. #include <string_view> // @manual
  37. #endif
  38. #include <folly/CpuId.h>
  39. #include <folly/Likely.h>
  40. #include <folly/Traits.h>
  41. #include <folly/detail/RangeCommon.h>
  42. #include <folly/detail/RangeSse42.h>
  43. // Ignore shadowing warnings within this file, so includers can use -Wshadow.
  44. FOLLY_PUSH_WARNING
  45. FOLLY_GNU_DISABLE_WARNING("-Wshadow")
  46. namespace folly {
  47. /**
  48. * Ubiquitous helper template for knowing what's a string.
  49. */
  50. template <class T>
  51. struct IsSomeString : std::false_type {};
  52. template <>
  53. struct IsSomeString<std::string> : std::true_type {};
  54. template <class Iter>
  55. class Range;
  56. /**
  57. * Finds the first occurrence of needle in haystack. The algorithm is on
  58. * average faster than O(haystack.size() * needle.size()) but not as fast
  59. * as Boyer-Moore. On the upside, it does not do any upfront
  60. * preprocessing and does not allocate memory.
  61. */
  62. template <
  63. class Iter,
  64. class Comp = std::equal_to<typename Range<Iter>::value_type>>
  65. inline size_t
  66. qfind(const Range<Iter>& haystack, const Range<Iter>& needle, Comp eq = Comp());
  67. /**
  68. * Finds the first occurrence of needle in haystack. The result is the
  69. * offset reported to the beginning of haystack, or string::npos if
  70. * needle wasn't found.
  71. */
  72. template <class Iter>
  73. size_t qfind(
  74. const Range<Iter>& haystack,
  75. const typename Range<Iter>::value_type& needle);
  76. /**
  77. * Finds the last occurrence of needle in haystack. The result is the
  78. * offset reported to the beginning of haystack, or string::npos if
  79. * needle wasn't found.
  80. */
  81. template <class Iter>
  82. size_t rfind(
  83. const Range<Iter>& haystack,
  84. const typename Range<Iter>::value_type& needle);
  85. /**
  86. * Finds the first occurrence of any element of needle in
  87. * haystack. The algorithm is O(haystack.size() * needle.size()).
  88. */
  89. template <class Iter>
  90. inline size_t qfind_first_of(
  91. const Range<Iter>& haystack,
  92. const Range<Iter>& needle);
  93. /**
  94. * Small internal helper - returns the value just before an iterator.
  95. */
  96. namespace detail {
  97. /**
  98. * For random-access iterators, the value before is simply i[-1].
  99. */
  100. template <class Iter>
  101. typename std::enable_if<
  102. std::is_same<
  103. typename std::iterator_traits<Iter>::iterator_category,
  104. std::random_access_iterator_tag>::value,
  105. typename std::iterator_traits<Iter>::reference>::type
  106. value_before(Iter i) {
  107. return i[-1];
  108. }
  109. /**
  110. * For all other iterators, we need to use the decrement operator.
  111. */
  112. template <class Iter>
  113. typename std::enable_if<
  114. !std::is_same<
  115. typename std::iterator_traits<Iter>::iterator_category,
  116. std::random_access_iterator_tag>::value,
  117. typename std::iterator_traits<Iter>::reference>::type
  118. value_before(Iter i) {
  119. return *--i;
  120. }
  121. /*
  122. * Use IsCharPointer<T>::type to enable const char* or char*.
  123. * Use IsCharPointer<T>::const_type to enable only const char*.
  124. */
  125. template <class T>
  126. struct IsCharPointer {};
  127. template <>
  128. struct IsCharPointer<char*> {
  129. typedef int type;
  130. };
  131. template <>
  132. struct IsCharPointer<const char*> {
  133. typedef int const_type;
  134. typedef int type;
  135. };
  136. } // namespace detail
  137. /**
  138. * Range abstraction keeping a pair of iterators. We couldn't use
  139. * boost's similar range abstraction because we need an API identical
  140. * with the former StringPiece class, which is used by a lot of other
  141. * code. This abstraction does fulfill the needs of boost's
  142. * range-oriented algorithms though.
  143. *
  144. * (Keep memory lifetime in mind when using this class, since it
  145. * doesn't manage the data it refers to - just like an iterator
  146. * wouldn't.)
  147. */
  148. template <class Iter>
  149. class Range {
  150. public:
  151. typedef std::size_t size_type;
  152. typedef Iter iterator;
  153. typedef Iter const_iterator;
  154. typedef typename std::remove_reference<
  155. typename std::iterator_traits<Iter>::reference>::type value_type;
  156. using difference_type = typename std::iterator_traits<Iter>::difference_type;
  157. typedef typename std::iterator_traits<Iter>::reference reference;
  158. /**
  159. * For MutableStringPiece and MutableByteRange we define StringPiece
  160. * and ByteRange as const_range_type (for everything else its just
  161. * identity). We do that to enable operations such as find with
  162. * args which are const.
  163. */
  164. typedef typename std::conditional<
  165. std::is_same<Iter, char*>::value ||
  166. std::is_same<Iter, unsigned char*>::value,
  167. Range<const value_type*>,
  168. Range<Iter>>::type const_range_type;
  169. typedef std::char_traits<typename std::remove_const<value_type>::type>
  170. traits_type;
  171. static const size_type npos;
  172. // Works for all iterators
  173. constexpr Range() : b_(), e_() {}
  174. constexpr Range(const Range&) = default;
  175. constexpr Range(Range&&) = default;
  176. public:
  177. // Works for all iterators
  178. constexpr Range(Iter start, Iter end) : b_(start), e_(end) {}
  179. // Works only for random-access iterators
  180. constexpr Range(Iter start, size_t size) : b_(start), e_(start + size) {}
  181. #if !__clang__ || __CLANG_PREREQ(3, 7) // Clang 3.6 crashes on this line
  182. /* implicit */ Range(std::nullptr_t) = delete;
  183. #endif
  184. constexpr /* implicit */ Range(Iter str)
  185. : b_(str), e_(str + constexpr_strlen(str)) {
  186. static_assert(
  187. std::is_same<int, typename detail::IsCharPointer<Iter>::type>::value,
  188. "This constructor is only available for character ranges");
  189. }
  190. template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
  191. /* implicit */ Range(const std::string& str)
  192. : b_(str.data()), e_(b_ + str.size()) {}
  193. template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
  194. Range(const std::string& str, std::string::size_type startFrom) {
  195. if (UNLIKELY(startFrom > str.size())) {
  196. throw_exception<std::out_of_range>("index out of range");
  197. }
  198. b_ = str.data() + startFrom;
  199. e_ = str.data() + str.size();
  200. }
  201. template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
  202. Range(
  203. const std::string& str,
  204. std::string::size_type startFrom,
  205. std::string::size_type size) {
  206. if (UNLIKELY(startFrom > str.size())) {
  207. throw_exception<std::out_of_range>("index out of range");
  208. }
  209. b_ = str.data() + startFrom;
  210. if (str.size() - startFrom < size) {
  211. e_ = str.data() + str.size();
  212. } else {
  213. e_ = b_ + size;
  214. }
  215. }
  216. Range(const Range& other, size_type first, size_type length = npos)
  217. : Range(other.subpiece(first, length)) {}
  218. template <
  219. class Container,
  220. class = typename std::enable_if<
  221. std::is_same<Iter, typename Container::const_pointer>::value>::type,
  222. class = decltype(
  223. Iter(std::declval<Container const&>().data()),
  224. Iter(
  225. std::declval<Container const&>().data() +
  226. std::declval<Container const&>().size()))>
  227. /* implicit */ constexpr Range(Container const& container)
  228. : b_(container.data()), e_(b_ + container.size()) {}
  229. template <
  230. class Container,
  231. class = typename std::enable_if<
  232. std::is_same<Iter, typename Container::const_pointer>::value>::type,
  233. class = decltype(
  234. Iter(std::declval<Container const&>().data()),
  235. Iter(
  236. std::declval<Container const&>().data() +
  237. std::declval<Container const&>().size()))>
  238. Range(Container const& container, typename Container::size_type startFrom) {
  239. auto const cdata = container.data();
  240. auto const csize = container.size();
  241. if (UNLIKELY(startFrom > csize)) {
  242. throw_exception<std::out_of_range>("index out of range");
  243. }
  244. b_ = cdata + startFrom;
  245. e_ = cdata + csize;
  246. }
  247. template <
  248. class Container,
  249. class = typename std::enable_if<
  250. std::is_same<Iter, typename Container::const_pointer>::value>::type,
  251. class = decltype(
  252. Iter(std::declval<Container const&>().data()),
  253. Iter(
  254. std::declval<Container const&>().data() +
  255. std::declval<Container const&>().size()))>
  256. Range(
  257. Container const& container,
  258. typename Container::size_type startFrom,
  259. typename Container::size_type size) {
  260. auto const cdata = container.data();
  261. auto const csize = container.size();
  262. if (UNLIKELY(startFrom > csize)) {
  263. throw_exception<std::out_of_range>("index out of range");
  264. }
  265. b_ = cdata + startFrom;
  266. if (csize - startFrom < size) {
  267. e_ = cdata + csize;
  268. } else {
  269. e_ = b_ + size;
  270. }
  271. }
  272. // Allow implicit conversion from Range<const char*> (aka StringPiece) to
  273. // Range<const unsigned char*> (aka ByteRange), as they're both frequently
  274. // used to represent ranges of bytes. Allow explicit conversion in the other
  275. // direction.
  276. template <
  277. class OtherIter,
  278. typename std::enable_if<
  279. (std::is_same<Iter, const unsigned char*>::value &&
  280. (std::is_same<OtherIter, const char*>::value ||
  281. std::is_same<OtherIter, char*>::value)),
  282. int>::type = 0>
  283. /* implicit */ Range(const Range<OtherIter>& other)
  284. : b_(reinterpret_cast<const unsigned char*>(other.begin())),
  285. e_(reinterpret_cast<const unsigned char*>(other.end())) {}
  286. template <
  287. class OtherIter,
  288. typename std::enable_if<
  289. (std::is_same<Iter, unsigned char*>::value &&
  290. std::is_same<OtherIter, char*>::value),
  291. int>::type = 0>
  292. /* implicit */ Range(const Range<OtherIter>& other)
  293. : b_(reinterpret_cast<unsigned char*>(other.begin())),
  294. e_(reinterpret_cast<unsigned char*>(other.end())) {}
  295. template <
  296. class OtherIter,
  297. typename std::enable_if<
  298. (std::is_same<Iter, const char*>::value &&
  299. (std::is_same<OtherIter, const unsigned char*>::value ||
  300. std::is_same<OtherIter, unsigned char*>::value)),
  301. int>::type = 0>
  302. explicit Range(const Range<OtherIter>& other)
  303. : b_(reinterpret_cast<const char*>(other.begin())),
  304. e_(reinterpret_cast<const char*>(other.end())) {}
  305. template <
  306. class OtherIter,
  307. typename std::enable_if<
  308. (std::is_same<Iter, char*>::value &&
  309. std::is_same<OtherIter, unsigned char*>::value),
  310. int>::type = 0>
  311. explicit Range(const Range<OtherIter>& other)
  312. : b_(reinterpret_cast<char*>(other.begin())),
  313. e_(reinterpret_cast<char*>(other.end())) {}
  314. // Allow implicit conversion from Range<From> to Range<To> if From is
  315. // implicitly convertible to To.
  316. template <
  317. class OtherIter,
  318. typename std::enable_if<
  319. (!std::is_same<Iter, OtherIter>::value &&
  320. std::is_convertible<OtherIter, Iter>::value),
  321. int>::type = 0>
  322. constexpr /* implicit */ Range(const Range<OtherIter>& other)
  323. : b_(other.begin()), e_(other.end()) {}
  324. // Allow explicit conversion from Range<From> to Range<To> if From is
  325. // explicitly convertible to To.
  326. template <
  327. class OtherIter,
  328. typename std::enable_if<
  329. (!std::is_same<Iter, OtherIter>::value &&
  330. !std::is_convertible<OtherIter, Iter>::value &&
  331. std::is_constructible<Iter, const OtherIter&>::value),
  332. int>::type = 0>
  333. constexpr explicit Range(const Range<OtherIter>& other)
  334. : b_(other.begin()), e_(other.end()) {}
  335. /**
  336. * Allow explicit construction of Range() from a std::array of a
  337. * convertible type.
  338. *
  339. * For instance, this allows constructing StringPiece from a
  340. * std::array<char, N> or a std::array<const char, N>
  341. */
  342. template <
  343. class T,
  344. size_t N,
  345. typename = typename std::enable_if<
  346. std::is_convertible<const T*, Iter>::value>::type>
  347. constexpr explicit Range(const std::array<T, N>& array)
  348. : b_{array.empty() ? nullptr : &array.at(0)},
  349. e_{array.empty() ? nullptr : &array.at(0) + N} {}
  350. template <
  351. class T,
  352. size_t N,
  353. typename =
  354. typename std::enable_if<std::is_convertible<T*, Iter>::value>::type>
  355. constexpr explicit Range(std::array<T, N>& array)
  356. : b_{array.empty() ? nullptr : &array.at(0)},
  357. e_{array.empty() ? nullptr : &array.at(0) + N} {}
  358. Range& operator=(const Range& rhs) & = default;
  359. Range& operator=(Range&& rhs) & = default;
  360. template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
  361. Range& operator=(std::string&& rhs) = delete;
  362. void clear() {
  363. b_ = Iter();
  364. e_ = Iter();
  365. }
  366. void assign(Iter start, Iter end) {
  367. b_ = start;
  368. e_ = end;
  369. }
  370. void reset(Iter start, size_type size) {
  371. b_ = start;
  372. e_ = start + size;
  373. }
  374. // Works only for Range<const char*>
  375. void reset(const std::string& str) {
  376. reset(str.data(), str.size());
  377. }
  378. constexpr size_type size() const {
  379. // It would be nice to assert(b_ <= e_) here. This can be achieved even
  380. // in a C++11 compatible constexpr function:
  381. // http://ericniebler.com/2014/09/27/assert-and-constexpr-in-cxx11/
  382. // Unfortunately current gcc versions have a bug causing it to reject
  383. // this check in a constexpr function:
  384. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71448
  385. return size_type(e_ - b_);
  386. }
  387. constexpr size_type walk_size() const {
  388. return size_type(std::distance(b_, e_));
  389. }
  390. constexpr bool empty() const {
  391. return b_ == e_;
  392. }
  393. constexpr Iter data() const {
  394. return b_;
  395. }
  396. constexpr Iter start() const {
  397. return b_;
  398. }
  399. constexpr Iter begin() const {
  400. return b_;
  401. }
  402. constexpr Iter end() const {
  403. return e_;
  404. }
  405. constexpr Iter cbegin() const {
  406. return b_;
  407. }
  408. constexpr Iter cend() const {
  409. return e_;
  410. }
  411. value_type& front() {
  412. assert(b_ < e_);
  413. return *b_;
  414. }
  415. value_type& back() {
  416. assert(b_ < e_);
  417. return detail::value_before(e_);
  418. }
  419. const value_type& front() const {
  420. assert(b_ < e_);
  421. return *b_;
  422. }
  423. const value_type& back() const {
  424. assert(b_ < e_);
  425. return detail::value_before(e_);
  426. }
  427. private:
  428. // It would be nice to be able to implicit convert to any target type
  429. // T for which either an (Iter, Iter) or (Iter, size_type) noexcept
  430. // constructor was available, and explicitly convert to any target
  431. // type for which those signatures were available but not noexcept.
  432. // The problem is that this creates ambiguity when there is also a
  433. // T constructor that takes a type U that is implicitly convertible
  434. // from Range.
  435. //
  436. // To avoid ambiguity, we need to avoid having explicit operator T
  437. // and implicit operator U coexist when T is constructible from U.
  438. // U cannot be deduced when searching for operator T (and C++ won't
  439. // perform an existential search for it), so we must limit the implicit
  440. // target types to a finite set that we can enumerate.
  441. //
  442. // At the moment the set of implicit target types consists of just
  443. // std::string_view (when it is available).
  444. #if FOLLY_HAS_STRING_VIEW
  445. using StringViewType =
  446. std::basic_string_view<std::remove_const_t<value_type>>;
  447. template <typename Target>
  448. using IsConstructibleViaStringView = StrictConjunction<
  449. std::is_constructible<StringViewType, Iter const&, size_type>,
  450. std::is_constructible<Target, StringViewType>>;
  451. #else
  452. template <typename Target>
  453. using IsConstructibleViaStringView = std::false_type;
  454. #endif
  455. public:
  456. /// explicit operator conversion to any compatible type
  457. ///
  458. /// A compatible type is one which is constructible with an iterator and a
  459. /// size (preferred), or a pair of iterators (fallback), passed by const-ref.
  460. ///
  461. /// Participates in overload resolution precisely when the target type is
  462. /// compatible. This allows std::is_constructible compile-time checks to work.
  463. template <
  464. typename Tgt,
  465. std::enable_if_t<
  466. std::is_constructible<Tgt, Iter const&, size_type>::value &&
  467. !IsConstructibleViaStringView<Tgt>::value,
  468. int> = 0>
  469. constexpr explicit operator Tgt() const noexcept(
  470. std::is_nothrow_constructible<Tgt, Iter const&, size_type>::value) {
  471. return Tgt(b_, walk_size());
  472. }
  473. template <
  474. typename Tgt,
  475. std::enable_if_t<
  476. !std::is_constructible<Tgt, Iter const&, size_type>::value &&
  477. std::is_constructible<Tgt, Iter const&, Iter const&>::value &&
  478. !IsConstructibleViaStringView<Tgt>::value,
  479. int> = 0>
  480. constexpr explicit operator Tgt() const noexcept(
  481. std::is_nothrow_constructible<Tgt, Iter const&, Iter const&>::value) {
  482. return Tgt(b_, e_);
  483. }
  484. #if FOLLY_HAS_STRING_VIEW
  485. /// implicit operator conversion to std::string_view
  486. template <
  487. typename Tgt,
  488. std::enable_if_t<
  489. StrictConjunction<
  490. std::is_same<Tgt, StringViewType>,
  491. std::is_constructible<StringViewType, Iter const&, size_type>>::
  492. value,
  493. int> = 0>
  494. constexpr operator Tgt() const noexcept(
  495. std::is_nothrow_constructible<Tgt, Iter const&, size_type>::value) {
  496. return Tgt(b_, walk_size());
  497. }
  498. #endif
  499. /// explicit non-operator conversion to any compatible type
  500. ///
  501. /// A compatible type is one which is constructible with an iterator and a
  502. /// size (preferred), or a pair of iterators (fallback), passed by const-ref.
  503. ///
  504. /// Participates in overload resolution precisely when the target type is
  505. /// compatible. This allows is_invocable compile-time checks to work.
  506. ///
  507. /// Provided in addition to the explicit operator conversion to permit passing
  508. /// additional arguments to the target type constructor. A canonical example
  509. /// of an additional argument might be an allocator, where the target type is
  510. /// some specialization of std::vector or std::basic_string in a context which
  511. /// requires a non-default-constructed allocator.
  512. template <typename Tgt, typename... Args>
  513. constexpr std::enable_if_t<
  514. std::is_constructible<Tgt, Iter const&, size_type>::value,
  515. Tgt>
  516. to(Args&&... args) const noexcept(
  517. std::is_nothrow_constructible<Tgt, Iter const&, size_type, Args&&...>::
  518. value) {
  519. return Tgt(b_, walk_size(), static_cast<Args&&>(args)...);
  520. }
  521. template <typename Tgt, typename... Args>
  522. constexpr std::enable_if_t<
  523. !std::is_constructible<Tgt, Iter const&, size_type>::value &&
  524. std::is_constructible<Tgt, Iter const&, Iter const&>::value,
  525. Tgt>
  526. to(Args&&... args) const noexcept(
  527. std::is_nothrow_constructible<Tgt, Iter const&, Iter const&, Args&&...>::
  528. value) {
  529. return Tgt(b_, e_, static_cast<Args&&>(args)...);
  530. }
  531. // Works only for Range<const char*> and Range<char*>
  532. std::string str() const {
  533. return to<std::string>();
  534. }
  535. std::string toString() const {
  536. return to<std::string>();
  537. }
  538. const_range_type castToConst() const {
  539. return const_range_type(*this);
  540. }
  541. // Works only for Range<const char*> and Range<char*>
  542. int compare(const const_range_type& o) const {
  543. const size_type tsize = this->size();
  544. const size_type osize = o.size();
  545. const size_type msize = std::min(tsize, osize);
  546. int r = traits_type::compare(data(), o.data(), msize);
  547. if (r == 0 && tsize != osize) {
  548. // We check the signed bit of the subtraction and bit shift it
  549. // to produce either 0 or 2. The subtraction yields the
  550. // comparison values of either -1 or 1.
  551. r = (static_cast<int>((osize - tsize) >> (CHAR_BIT * sizeof(size_t) - 1))
  552. << 1) -
  553. 1;
  554. }
  555. return r;
  556. }
  557. value_type& operator[](size_t i) {
  558. assert(i < size());
  559. return b_[i];
  560. }
  561. const value_type& operator[](size_t i) const {
  562. assert(i < size());
  563. return b_[i];
  564. }
  565. value_type& at(size_t i) {
  566. if (i >= size()) {
  567. throw_exception<std::out_of_range>("index out of range");
  568. }
  569. return b_[i];
  570. }
  571. const value_type& at(size_t i) const {
  572. if (i >= size()) {
  573. throw_exception<std::out_of_range>("index out of range");
  574. }
  575. return b_[i];
  576. }
  577. // Do NOT use this function, which was left behind for backwards
  578. // compatibility. Use SpookyHashV2 instead -- it is faster, and produces
  579. // a 64-bit hash, which means dramatically fewer collisions in large maps.
  580. // (The above advice does not apply if you are targeting a 32-bit system.)
  581. //
  582. // Works only for Range<const char*> and Range<char*>
  583. //
  584. //
  585. // ** WANT TO GET RID OF THIS LINT? **
  586. //
  587. // A) Use a better hash function (*cough*folly::Hash*cough*), but
  588. // only if you don't serialize data in a format that depends on
  589. // this formula (ie the writer and reader assume this exact hash
  590. // function is used).
  591. //
  592. // B) If you have to use this exact function then make your own hasher
  593. // object and copy the body over (see thrift example: D3972362).
  594. // https://github.com/facebook/fbthrift/commit/f8ed502e24ab4a32a9d5f266580
  595. [[deprecated(
  596. "Replace with folly::Hash if the hash is not serialized")]] uint32_t
  597. hash() const {
  598. // Taken from fbi/nstring.h:
  599. // Quick and dirty bernstein hash...fine for short ascii strings
  600. uint32_t hash = 5381;
  601. for (size_t ix = 0; ix < size(); ix++) {
  602. hash = ((hash << 5) + hash) + b_[ix];
  603. }
  604. return hash;
  605. }
  606. void advance(size_type n) {
  607. if (UNLIKELY(n > size())) {
  608. throw_exception<std::out_of_range>("index out of range");
  609. }
  610. b_ += n;
  611. }
  612. void subtract(size_type n) {
  613. if (UNLIKELY(n > size())) {
  614. throw_exception<std::out_of_range>("index out of range");
  615. }
  616. e_ -= n;
  617. }
  618. Range subpiece(size_type first, size_type length = npos) const {
  619. if (UNLIKELY(first > size())) {
  620. throw_exception<std::out_of_range>("index out of range");
  621. }
  622. return Range(b_ + first, std::min(length, size() - first));
  623. }
  624. // unchecked versions
  625. void uncheckedAdvance(size_type n) {
  626. assert(n <= size());
  627. b_ += n;
  628. }
  629. void uncheckedSubtract(size_type n) {
  630. assert(n <= size());
  631. e_ -= n;
  632. }
  633. Range uncheckedSubpiece(size_type first, size_type length = npos) const {
  634. assert(first <= size());
  635. return Range(b_ + first, std::min(length, size() - first));
  636. }
  637. void pop_front() {
  638. assert(b_ < e_);
  639. ++b_;
  640. }
  641. void pop_back() {
  642. assert(b_ < e_);
  643. --e_;
  644. }
  645. // string work-alike functions
  646. size_type find(const_range_type str) const {
  647. return qfind(castToConst(), str);
  648. }
  649. size_type find(const_range_type str, size_t pos) const {
  650. if (pos > size()) {
  651. return std::string::npos;
  652. }
  653. size_t ret = qfind(castToConst().subpiece(pos), str);
  654. return ret == npos ? ret : ret + pos;
  655. }
  656. size_type find(Iter s, size_t pos, size_t n) const {
  657. if (pos > size()) {
  658. return std::string::npos;
  659. }
  660. auto forFinding = castToConst();
  661. size_t ret = qfind(
  662. pos ? forFinding.subpiece(pos) : forFinding, const_range_type(s, n));
  663. return ret == npos ? ret : ret + pos;
  664. }
  665. // Works only for Range<(const) (unsigned) char*> which have Range(Iter) ctor
  666. size_type find(const Iter s) const {
  667. return qfind(castToConst(), const_range_type(s));
  668. }
  669. // Works only for Range<(const) (unsigned) char*> which have Range(Iter) ctor
  670. size_type find(const Iter s, size_t pos) const {
  671. if (pos > size()) {
  672. return std::string::npos;
  673. }
  674. size_type ret = qfind(castToConst().subpiece(pos), const_range_type(s));
  675. return ret == npos ? ret : ret + pos;
  676. }
  677. size_type find(value_type c) const {
  678. return qfind(castToConst(), c);
  679. }
  680. size_type rfind(value_type c) const {
  681. return folly::rfind(castToConst(), c);
  682. }
  683. size_type find(value_type c, size_t pos) const {
  684. if (pos > size()) {
  685. return std::string::npos;
  686. }
  687. size_type ret = qfind(castToConst().subpiece(pos), c);
  688. return ret == npos ? ret : ret + pos;
  689. }
  690. size_type find_first_of(const_range_type needles) const {
  691. return qfind_first_of(castToConst(), needles);
  692. }
  693. size_type find_first_of(const_range_type needles, size_t pos) const {
  694. if (pos > size()) {
  695. return std::string::npos;
  696. }
  697. size_type ret = qfind_first_of(castToConst().subpiece(pos), needles);
  698. return ret == npos ? ret : ret + pos;
  699. }
  700. // Works only for Range<(const) (unsigned) char*> which have Range(Iter) ctor
  701. size_type find_first_of(Iter needles) const {
  702. return find_first_of(const_range_type(needles));
  703. }
  704. // Works only for Range<(const) (unsigned) char*> which have Range(Iter) ctor
  705. size_type find_first_of(Iter needles, size_t pos) const {
  706. return find_first_of(const_range_type(needles), pos);
  707. }
  708. size_type find_first_of(Iter needles, size_t pos, size_t n) const {
  709. return find_first_of(const_range_type(needles, n), pos);
  710. }
  711. size_type find_first_of(value_type c) const {
  712. return find(c);
  713. }
  714. size_type find_first_of(value_type c, size_t pos) const {
  715. return find(c, pos);
  716. }
  717. /**
  718. * Determine whether the range contains the given subrange or item.
  719. *
  720. * Note: Call find() directly if the index is needed.
  721. */
  722. bool contains(const const_range_type& other) const {
  723. return find(other) != std::string::npos;
  724. }
  725. bool contains(const value_type& other) const {
  726. return find(other) != std::string::npos;
  727. }
  728. void swap(Range& rhs) {
  729. std::swap(b_, rhs.b_);
  730. std::swap(e_, rhs.e_);
  731. }
  732. /**
  733. * Does this Range start with another range?
  734. */
  735. bool startsWith(const const_range_type& other) const {
  736. return size() >= other.size() &&
  737. castToConst().subpiece(0, other.size()) == other;
  738. }
  739. bool startsWith(value_type c) const {
  740. return !empty() && front() == c;
  741. }
  742. template <class Comp>
  743. bool startsWith(const const_range_type& other, Comp&& eq) const {
  744. if (size() < other.size()) {
  745. return false;
  746. }
  747. auto const trunc = subpiece(0, other.size());
  748. return std::equal(
  749. trunc.begin(), trunc.end(), other.begin(), std::forward<Comp>(eq));
  750. }
  751. /**
  752. * Does this Range end with another range?
  753. */
  754. bool endsWith(const const_range_type& other) const {
  755. return size() >= other.size() &&
  756. castToConst().subpiece(size() - other.size()) == other;
  757. }
  758. bool endsWith(value_type c) const {
  759. return !empty() && back() == c;
  760. }
  761. template <class Comp>
  762. bool endsWith(const const_range_type& other, Comp&& eq) const {
  763. if (size() < other.size()) {
  764. return false;
  765. }
  766. auto const trunc = subpiece(size() - other.size());
  767. return std::equal(
  768. trunc.begin(), trunc.end(), other.begin(), std::forward<Comp>(eq));
  769. }
  770. template <class Comp>
  771. bool equals(const const_range_type& other, Comp&& eq) const {
  772. return size() == other.size() &&
  773. std::equal(begin(), end(), other.begin(), std::forward<Comp>(eq));
  774. }
  775. /**
  776. * Remove the items in [b, e), as long as this subrange is at the beginning
  777. * or end of the Range.
  778. *
  779. * Required for boost::algorithm::trim()
  780. */
  781. void erase(Iter b, Iter e) {
  782. if (b == b_) {
  783. b_ = e;
  784. } else if (e == e_) {
  785. e_ = b;
  786. } else {
  787. throw_exception<std::out_of_range>("index out of range");
  788. }
  789. }
  790. /**
  791. * Remove the given prefix and return true if the range starts with the given
  792. * prefix; return false otherwise.
  793. */
  794. bool removePrefix(const const_range_type& prefix) {
  795. return startsWith(prefix) && (b_ += prefix.size(), true);
  796. }
  797. bool removePrefix(value_type prefix) {
  798. return startsWith(prefix) && (++b_, true);
  799. }
  800. /**
  801. * Remove the given suffix and return true if the range ends with the given
  802. * suffix; return false otherwise.
  803. */
  804. bool removeSuffix(const const_range_type& suffix) {
  805. return endsWith(suffix) && (e_ -= suffix.size(), true);
  806. }
  807. bool removeSuffix(value_type suffix) {
  808. return endsWith(suffix) && (--e_, true);
  809. }
  810. /**
  811. * Replaces the content of the range, starting at position 'pos', with
  812. * contents of 'replacement'. Entire 'replacement' must fit into the
  813. * range. Returns false if 'replacements' does not fit. Example use:
  814. *
  815. * char in[] = "buffer";
  816. * auto msp = MutablesStringPiece(input);
  817. * EXPECT_TRUE(msp.replaceAt(2, "tt"));
  818. * EXPECT_EQ(msp, "butter");
  819. *
  820. * // not enough space
  821. * EXPECT_FALSE(msp.replace(msp.size() - 1, "rr"));
  822. * EXPECT_EQ(msp, "butter"); // unchanged
  823. */
  824. bool replaceAt(size_t pos, const_range_type replacement) {
  825. if (size() < pos + replacement.size()) {
  826. return false;
  827. }
  828. std::copy(replacement.begin(), replacement.end(), begin() + pos);
  829. return true;
  830. }
  831. /**
  832. * Replaces all occurences of 'source' with 'dest'. Returns number
  833. * of replacements made. Source and dest have to have the same
  834. * length. Throws if the lengths are different. If 'source' is a
  835. * pattern that is overlapping with itself, we perform sequential
  836. * replacement: "aaaaaaa".replaceAll("aa", "ba") --> "bababaa"
  837. *
  838. * Example use:
  839. *
  840. * char in[] = "buffer";
  841. * auto msp = MutablesStringPiece(input);
  842. * EXPECT_EQ(msp.replaceAll("ff","tt"), 1);
  843. * EXPECT_EQ(msp, "butter");
  844. */
  845. size_t replaceAll(const_range_type source, const_range_type dest) {
  846. if (source.size() != dest.size()) {
  847. throw_exception<std::invalid_argument>(
  848. "replacement must have the same size as source");
  849. }
  850. if (dest.empty()) {
  851. return 0;
  852. }
  853. size_t pos = 0;
  854. size_t num_replaced = 0;
  855. size_type found = std::string::npos;
  856. while ((found = find(source, pos)) != std::string::npos) {
  857. replaceAt(found, dest);
  858. pos += source.size();
  859. ++num_replaced;
  860. }
  861. return num_replaced;
  862. }
  863. /**
  864. * Splits this `Range` `[b, e)` in the position `i` dictated by the next
  865. * occurence of `delimiter`.
  866. *
  867. * Returns a new `Range` `[b, i)` and adjusts this range to start right after
  868. * the delimiter's position. This range will be empty if the delimiter is not
  869. * found. If called on an empty `Range`, both this and the returned `Range`
  870. * will be empty.
  871. *
  872. * Example:
  873. *
  874. * folly::StringPiece s("sample string for split_next");
  875. * auto p = s.split_step(' ');
  876. *
  877. * // prints "string for split_next"
  878. * cout << s << endl;
  879. *
  880. * // prints "sample"
  881. * cout << p << endl;
  882. *
  883. * Example 2:
  884. *
  885. * void tokenize(StringPiece s, char delimiter) {
  886. * while (!s.empty()) {
  887. * cout << s.split_step(delimiter);
  888. * }
  889. * }
  890. *
  891. * @author: Marcelo Juchem <marcelo@fb.com>
  892. */
  893. Range split_step(value_type delimiter) {
  894. auto i = std::find(b_, e_, delimiter);
  895. Range result(b_, i);
  896. b_ = i == e_ ? e_ : std::next(i);
  897. return result;
  898. }
  899. Range split_step(Range delimiter) {
  900. auto i = find(delimiter);
  901. Range result(b_, i == std::string::npos ? size() : i);
  902. b_ = result.end() == e_
  903. ? e_
  904. : std::next(
  905. result.end(),
  906. typename std::iterator_traits<Iter>::difference_type(
  907. delimiter.size()));
  908. return result;
  909. }
  910. /**
  911. * Convenience method that calls `split_step()` and passes the result to a
  912. * functor, returning whatever the functor does. Any additional arguments
  913. * `args` passed to this function are perfectly forwarded to the functor.
  914. *
  915. * Say you have a functor with this signature:
  916. *
  917. * Foo fn(Range r) { }
  918. *
  919. * `split_step()`'s return type will be `Foo`. It works just like:
  920. *
  921. * auto result = fn(myRange.split_step(' '));
  922. *
  923. * A functor returning `void` is also supported.
  924. *
  925. * Example:
  926. *
  927. * void do_some_parsing(folly::StringPiece s) {
  928. * auto version = s.split_step(' ', [&](folly::StringPiece x) {
  929. * if (x.empty()) {
  930. * throw std::invalid_argument("empty string");
  931. * }
  932. * return std::strtoull(x.begin(), x.end(), 16);
  933. * });
  934. *
  935. * // ...
  936. * }
  937. *
  938. * struct Foo {
  939. * void parse(folly::StringPiece s) {
  940. * s.split_step(' ', parse_field, bar, 10);
  941. * s.split_step('\t', parse_field, baz, 20);
  942. *
  943. * auto const kludge = [](folly::StringPiece x, int &out, int def) {
  944. * if (x == "null") {
  945. * out = 0;
  946. * } else {
  947. * parse_field(x, out, def);
  948. * }
  949. * };
  950. *
  951. * s.split_step('\t', kludge, gaz);
  952. * s.split_step(' ', kludge, foo);
  953. * }
  954. *
  955. * private:
  956. * int bar;
  957. * int baz;
  958. * int gaz;
  959. * int foo;
  960. *
  961. * static parse_field(folly::StringPiece s, int &out, int def) {
  962. * try {
  963. * out = folly::to<int>(s);
  964. * } catch (std::exception const &) {
  965. * value = def;
  966. * }
  967. * }
  968. * };
  969. *
  970. * @author: Marcelo Juchem <marcelo@fb.com>
  971. */
  972. template <typename TProcess, typename... Args>
  973. auto split_step(value_type delimiter, TProcess&& process, Args&&... args)
  974. -> decltype(process(std::declval<Range>(), std::forward<Args>(args)...)) {
  975. return process(split_step(delimiter), std::forward<Args>(args)...);
  976. }
  977. template <typename TProcess, typename... Args>
  978. auto split_step(Range delimiter, TProcess&& process, Args&&... args)
  979. -> decltype(process(std::declval<Range>(), std::forward<Args>(args)...)) {
  980. return process(split_step(delimiter), std::forward<Args>(args)...);
  981. }
  982. private:
  983. Iter b_, e_;
  984. };
  985. template <class Iter>
  986. const typename Range<Iter>::size_type Range<Iter>::npos = std::string::npos;
  987. template <class Iter>
  988. void swap(Range<Iter>& lhs, Range<Iter>& rhs) {
  989. lhs.swap(rhs);
  990. }
  991. /**
  992. * Create a range from two iterators, with type deduction.
  993. */
  994. template <class Iter>
  995. constexpr Range<Iter> range(Iter first, Iter last) {
  996. return Range<Iter>(first, last);
  997. }
  998. /*
  999. * Creates a range to reference the contents of a contiguous-storage container.
  1000. */
  1001. // Use pointers for types with '.data()' member
  1002. template <class Collection>
  1003. constexpr auto range(Collection& v) -> Range<decltype(v.data())> {
  1004. return Range<decltype(v.data())>(v.data(), v.data() + v.size());
  1005. }
  1006. template <class Collection>
  1007. constexpr auto range(Collection const& v) -> Range<decltype(v.data())> {
  1008. return Range<decltype(v.data())>(v.data(), v.data() + v.size());
  1009. }
  1010. template <class Collection>
  1011. constexpr auto crange(Collection const& v) -> Range<decltype(v.data())> {
  1012. return Range<decltype(v.data())>(v.data(), v.data() + v.size());
  1013. }
  1014. template <class T, size_t n>
  1015. constexpr Range<T*> range(T (&array)[n]) {
  1016. return Range<T*>(array, array + n);
  1017. }
  1018. template <class T, size_t n>
  1019. constexpr Range<T const*> range(T const (&array)[n]) {
  1020. return Range<T const*>(array, array + n);
  1021. }
  1022. template <class T, size_t n>
  1023. constexpr Range<T const*> crange(T const (&array)[n]) {
  1024. return Range<T const*>(array, array + n);
  1025. }
  1026. template <class T, size_t n>
  1027. constexpr Range<T*> range(std::array<T, n>& array) {
  1028. return Range<T*>{array};
  1029. }
  1030. template <class T, size_t n>
  1031. constexpr Range<T const*> range(std::array<T, n> const& array) {
  1032. return Range<T const*>{array};
  1033. }
  1034. template <class T, size_t n>
  1035. constexpr Range<T const*> crange(std::array<T, n> const& array) {
  1036. return Range<T const*>{array};
  1037. }
  1038. typedef Range<const char*> StringPiece;
  1039. typedef Range<char*> MutableStringPiece;
  1040. typedef Range<const unsigned char*> ByteRange;
  1041. typedef Range<unsigned char*> MutableByteRange;
  1042. template <class C>
  1043. std::basic_ostream<C>& operator<<(
  1044. std::basic_ostream<C>& os,
  1045. Range<C const*> piece) {
  1046. using StreamSize = decltype(os.width());
  1047. os.write(piece.start(), static_cast<StreamSize>(piece.size()));
  1048. return os;
  1049. }
  1050. template <class C>
  1051. std::basic_ostream<C>& operator<<(std::basic_ostream<C>& os, Range<C*> piece) {
  1052. using StreamSize = decltype(os.width());
  1053. os.write(piece.start(), static_cast<StreamSize>(piece.size()));
  1054. return os;
  1055. }
  1056. /**
  1057. * Templated comparison operators
  1058. */
  1059. template <class Iter>
  1060. inline bool operator==(const Range<Iter>& lhs, const Range<Iter>& rhs) {
  1061. return lhs.size() == rhs.size() && lhs.compare(rhs) == 0;
  1062. }
  1063. template <class Iter>
  1064. inline bool operator!=(const Range<Iter>& lhs, const Range<Iter>& rhs) {
  1065. return !(operator==(lhs, rhs));
  1066. }
  1067. template <class Iter>
  1068. inline bool operator<(const Range<Iter>& lhs, const Range<Iter>& rhs) {
  1069. return lhs.compare(rhs) < 0;
  1070. }
  1071. template <class Iter>
  1072. inline bool operator<=(const Range<Iter>& lhs, const Range<Iter>& rhs) {
  1073. return lhs.compare(rhs) <= 0;
  1074. }
  1075. template <class Iter>
  1076. inline bool operator>(const Range<Iter>& lhs, const Range<Iter>& rhs) {
  1077. return lhs.compare(rhs) > 0;
  1078. }
  1079. template <class Iter>
  1080. inline bool operator>=(const Range<Iter>& lhs, const Range<Iter>& rhs) {
  1081. return lhs.compare(rhs) >= 0;
  1082. }
  1083. /**
  1084. * Specializations of comparison operators for StringPiece
  1085. */
  1086. namespace detail {
  1087. template <class A, class B>
  1088. struct ComparableAsStringPiece {
  1089. enum {
  1090. value = (std::is_convertible<A, StringPiece>::value &&
  1091. std::is_same<B, StringPiece>::value) ||
  1092. (std::is_convertible<B, StringPiece>::value &&
  1093. std::is_same<A, StringPiece>::value)
  1094. };
  1095. };
  1096. } // namespace detail
  1097. /**
  1098. * operator== through conversion for Range<const char*>
  1099. */
  1100. template <class T, class U>
  1101. _t<std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>>
  1102. operator==(const T& lhs, const U& rhs) {
  1103. return StringPiece(lhs) == StringPiece(rhs);
  1104. }
  1105. /**
  1106. * operator!= through conversion for Range<const char*>
  1107. */
  1108. template <class T, class U>
  1109. _t<std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>>
  1110. operator!=(const T& lhs, const U& rhs) {
  1111. return StringPiece(lhs) != StringPiece(rhs);
  1112. }
  1113. /**
  1114. * operator< through conversion for Range<const char*>
  1115. */
  1116. template <class T, class U>
  1117. _t<std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>>
  1118. operator<(const T& lhs, const U& rhs) {
  1119. return StringPiece(lhs) < StringPiece(rhs);
  1120. }
  1121. /**
  1122. * operator> through conversion for Range<const char*>
  1123. */
  1124. template <class T, class U>
  1125. _t<std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>>
  1126. operator>(const T& lhs, const U& rhs) {
  1127. return StringPiece(lhs) > StringPiece(rhs);
  1128. }
  1129. /**
  1130. * operator< through conversion for Range<const char*>
  1131. */
  1132. template <class T, class U>
  1133. _t<std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>>
  1134. operator<=(const T& lhs, const U& rhs) {
  1135. return StringPiece(lhs) <= StringPiece(rhs);
  1136. }
  1137. /**
  1138. * operator> through conversion for Range<const char*>
  1139. */
  1140. template <class T, class U>
  1141. _t<std::enable_if<detail::ComparableAsStringPiece<T, U>::value, bool>>
  1142. operator>=(const T& lhs, const U& rhs) {
  1143. return StringPiece(lhs) >= StringPiece(rhs);
  1144. }
  1145. /**
  1146. * Finds substrings faster than brute force by borrowing from Boyer-Moore
  1147. */
  1148. template <class Iter, class Comp>
  1149. size_t qfind(const Range<Iter>& haystack, const Range<Iter>& needle, Comp eq) {
  1150. // Don't use std::search, use a Boyer-Moore-like trick by comparing
  1151. // the last characters first
  1152. auto const nsize = needle.size();
  1153. if (haystack.size() < nsize) {
  1154. return std::string::npos;
  1155. }
  1156. if (!nsize) {
  1157. return 0;
  1158. }
  1159. auto const nsize_1 = nsize - 1;
  1160. auto const lastNeedle = needle[nsize_1];
  1161. // Boyer-Moore skip value for the last char in the needle. Zero is
  1162. // not a valid value; skip will be computed the first time it's
  1163. // needed.
  1164. std::string::size_type skip = 0;
  1165. auto i = haystack.begin();
  1166. auto iEnd = haystack.end() - nsize_1;
  1167. while (i < iEnd) {
  1168. // Boyer-Moore: match the last element in the needle
  1169. while (!eq(i[nsize_1], lastNeedle)) {
  1170. if (++i == iEnd) {
  1171. // not found
  1172. return std::string::npos;
  1173. }
  1174. }
  1175. // Here we know that the last char matches
  1176. // Continue in pedestrian mode
  1177. for (size_t j = 0;;) {
  1178. assert(j < nsize);
  1179. if (!eq(i[j], needle[j])) {
  1180. // Not found, we can skip
  1181. // Compute the skip value lazily
  1182. if (skip == 0) {
  1183. skip = 1;
  1184. while (skip <= nsize_1 && !eq(needle[nsize_1 - skip], lastNeedle)) {
  1185. ++skip;
  1186. }
  1187. }
  1188. i += skip;
  1189. break;
  1190. }
  1191. // Check if done searching
  1192. if (++j == nsize) {
  1193. // Yay
  1194. return size_t(i - haystack.begin());
  1195. }
  1196. }
  1197. }
  1198. return std::string::npos;
  1199. }
  1200. namespace detail {
  1201. inline size_t qfind_first_byte_of(
  1202. const StringPiece haystack,
  1203. const StringPiece needles) {
  1204. static auto const qfind_first_byte_of_fn = folly::CpuId().sse42()
  1205. ? qfind_first_byte_of_sse42
  1206. : qfind_first_byte_of_nosse;
  1207. return qfind_first_byte_of_fn(haystack, needles);
  1208. }
  1209. } // namespace detail
  1210. template <class Iter, class Comp>
  1211. size_t qfind_first_of(
  1212. const Range<Iter>& haystack,
  1213. const Range<Iter>& needles,
  1214. Comp eq) {
  1215. auto ret = std::find_first_of(
  1216. haystack.begin(), haystack.end(), needles.begin(), needles.end(), eq);
  1217. return ret == haystack.end() ? std::string::npos : ret - haystack.begin();
  1218. }
  1219. struct AsciiCaseSensitive {
  1220. bool operator()(char lhs, char rhs) const {
  1221. return lhs == rhs;
  1222. }
  1223. };
  1224. /**
  1225. * Check if two ascii characters are case insensitive equal.
  1226. * The difference between the lower/upper case characters are the 6-th bit.
  1227. * We also check they are alpha chars, in case of xor = 32.
  1228. */
  1229. struct AsciiCaseInsensitive {
  1230. bool operator()(char lhs, char rhs) const {
  1231. char k = lhs ^ rhs;
  1232. if (k == 0) {
  1233. return true;
  1234. }
  1235. if (k != 32) {
  1236. return false;
  1237. }
  1238. k = lhs | rhs;
  1239. return (k >= 'a' && k <= 'z');
  1240. }
  1241. };
  1242. template <class Iter>
  1243. size_t qfind(
  1244. const Range<Iter>& haystack,
  1245. const typename Range<Iter>::value_type& needle) {
  1246. auto pos = std::find(haystack.begin(), haystack.end(), needle);
  1247. return pos == haystack.end() ? std::string::npos : pos - haystack.data();
  1248. }
  1249. template <class Iter>
  1250. size_t rfind(
  1251. const Range<Iter>& haystack,
  1252. const typename Range<Iter>::value_type& needle) {
  1253. for (auto i = haystack.size(); i-- > 0;) {
  1254. if (haystack[i] == needle) {
  1255. return i;
  1256. }
  1257. }
  1258. return std::string::npos;
  1259. }
  1260. // specialization for StringPiece
  1261. template <>
  1262. inline size_t qfind(const Range<const char*>& haystack, const char& needle) {
  1263. // memchr expects a not-null pointer, early return if the range is empty.
  1264. if (haystack.empty()) {
  1265. return std::string::npos;
  1266. }
  1267. auto pos = static_cast<const char*>(
  1268. ::memchr(haystack.data(), needle, haystack.size()));
  1269. return pos == nullptr ? std::string::npos : pos - haystack.data();
  1270. }
  1271. template <>
  1272. inline size_t rfind(const Range<const char*>& haystack, const char& needle) {
  1273. // memchr expects a not-null pointer, early return if the range is empty.
  1274. if (haystack.empty()) {
  1275. return std::string::npos;
  1276. }
  1277. auto pos = static_cast<const char*>(
  1278. ::memrchr(haystack.data(), needle, haystack.size()));
  1279. return pos == nullptr ? std::string::npos : pos - haystack.data();
  1280. }
  1281. // specialization for ByteRange
  1282. template <>
  1283. inline size_t qfind(
  1284. const Range<const unsigned char*>& haystack,
  1285. const unsigned char& needle) {
  1286. // memchr expects a not-null pointer, early return if the range is empty.
  1287. if (haystack.empty()) {
  1288. return std::string::npos;
  1289. }
  1290. auto pos = static_cast<const unsigned char*>(
  1291. ::memchr(haystack.data(), needle, haystack.size()));
  1292. return pos == nullptr ? std::string::npos : pos - haystack.data();
  1293. }
  1294. template <>
  1295. inline size_t rfind(
  1296. const Range<const unsigned char*>& haystack,
  1297. const unsigned char& needle) {
  1298. // memchr expects a not-null pointer, early return if the range is empty.
  1299. if (haystack.empty()) {
  1300. return std::string::npos;
  1301. }
  1302. auto pos = static_cast<const unsigned char*>(
  1303. ::memrchr(haystack.data(), needle, haystack.size()));
  1304. return pos == nullptr ? std::string::npos : pos - haystack.data();
  1305. }
  1306. template <class Iter>
  1307. size_t qfind_first_of(const Range<Iter>& haystack, const Range<Iter>& needles) {
  1308. return qfind_first_of(haystack, needles, AsciiCaseSensitive());
  1309. }
  1310. // specialization for StringPiece
  1311. template <>
  1312. inline size_t qfind_first_of(
  1313. const Range<const char*>& haystack,
  1314. const Range<const char*>& needles) {
  1315. return detail::qfind_first_byte_of(haystack, needles);
  1316. }
  1317. // specialization for ByteRange
  1318. template <>
  1319. inline size_t qfind_first_of(
  1320. const Range<const unsigned char*>& haystack,
  1321. const Range<const unsigned char*>& needles) {
  1322. return detail::qfind_first_byte_of(
  1323. StringPiece(haystack), StringPiece(needles));
  1324. }
  1325. template <class Key, class Enable>
  1326. struct hasher;
  1327. template <class T>
  1328. struct hasher<
  1329. folly::Range<T*>,
  1330. typename std::enable_if<std::is_pod<T>::value, void>::type> {
  1331. using folly_is_avalanching = std::true_type;
  1332. size_t operator()(folly::Range<T*> r) const {
  1333. return hash::SpookyHashV2::Hash64(r.begin(), r.size() * sizeof(T), 0);
  1334. }
  1335. };
  1336. /**
  1337. * _sp is a user-defined literal suffix to make an appropriate Range
  1338. * specialization from a literal string.
  1339. *
  1340. * Modeled after C++17's `sv` suffix.
  1341. */
  1342. inline namespace literals {
  1343. inline namespace string_piece_literals {
  1344. constexpr Range<char const*> operator"" _sp(
  1345. char const* str,
  1346. size_t len) noexcept {
  1347. return Range<char const*>(str, len);
  1348. }
  1349. constexpr Range<char16_t const*> operator"" _sp(
  1350. char16_t const* str,
  1351. size_t len) noexcept {
  1352. return Range<char16_t const*>(str, len);
  1353. }
  1354. constexpr Range<char32_t const*> operator"" _sp(
  1355. char32_t const* str,
  1356. size_t len) noexcept {
  1357. return Range<char32_t const*>(str, len);
  1358. }
  1359. constexpr Range<wchar_t const*> operator"" _sp(
  1360. wchar_t const* str,
  1361. size_t len) noexcept {
  1362. return Range<wchar_t const*>(str, len);
  1363. }
  1364. } // namespace string_piece_literals
  1365. } // namespace literals
  1366. } // namespace folly
  1367. FOLLY_POP_WARNING
  1368. FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(folly::Range)