LCOV - code coverage report
Current view: top level - include/llvm/Demangle - StringView.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 32 37 86.5 %
Date: 2018-10-20 13:21:21 Functions: 4 9 44.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===--- StringView.h -------------------------------------------*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //
       9             : // This file contains a limited version of LLVM's StringView class.  It is
      10             : // copied here so that LLVMDemangle need not take a dependency on LLVMSupport.
      11             : //===----------------------------------------------------------------------===//
      12             : 
      13             : #ifndef LLVM_DEMANGLE_STRINGVIEW_H
      14             : #define LLVM_DEMANGLE_STRINGVIEW_H
      15             : 
      16             : #include <algorithm>
      17             : #include <cassert>
      18             : #include <cstring>
      19             : 
      20             : class StringView {
      21             :   const char *First;
      22             :   const char *Last;
      23             : 
      24             : public:
      25             :   static const size_t npos = ~size_t(0);
      26             : 
      27             :   template <size_t N>
      28             :   StringView(const char (&Str)[N]) : First(Str), Last(Str + N - 1) {}
      29             :   StringView(const char *First_, const char *Last_)
      30        4048 :       : First(First_), Last(Last_) {}
      31             :   StringView(const char *First_, size_t Len)
      32         669 :       : First(First_), Last(First_ + Len) {}
      33       38525 :   StringView(const char *Str) : First(Str), Last(Str + std::strlen(Str)) {}
      34        4392 :   StringView() : First(nullptr), Last(nullptr) {}
      35             : 
      36             :   StringView substr(size_t From) const {
      37           2 :     return StringView(begin() + From, size() - From);
      38             :   }
      39             : 
      40         429 :   size_t find(char C, size_t From = 0) const {
      41        1287 :     size_t FindBegin = std::min(From, size());
      42             :     // Avoid calling memchr with nullptr.
      43         429 :     if (FindBegin < size()) {
      44             :       // Just forward to memchr, which is faster than a hand-rolled loop.
      45         858 :       if (const void *P = ::memchr(First + FindBegin, C, size() - FindBegin))
      46         429 :         return static_cast<const char *>(P) - First;
      47             :     }
      48             :     return npos;
      49             :   }
      50             : 
      51             :   StringView substr(size_t From, size_t To) const {
      52          57 :     if (To >= size())
      53           0 :       To = size() - 1;
      54        1479 :     if (From >= size())
      55             :       From = size() - 1;
      56        1536 :     return StringView(First + From, First + To);
      57             :   }
      58             : 
      59             :   StringView dropFront(size_t N = 1) const {
      60        1847 :     if (N >= size())
      61             :       N = size();
      62        2516 :     return StringView(First + N, Last);
      63             :   }
      64             : 
      65             :   StringView dropBack(size_t N = 1) const {
      66             :     if (N >= size())
      67             :       N = size();
      68           7 :     return StringView(First, Last - N);
      69             :   }
      70             : 
      71             :   char front() const {
      72             :     assert(!empty());
      73        7929 :     return *begin();
      74             :   }
      75             : 
      76             :   char back() const {
      77             :     assert(!empty());
      78           7 :     return *(end() - 1);
      79             :   }
      80             : 
      81           0 :   char popFront() {
      82             :     assert(!empty());
      83        4036 :     return *First++;
      84             :   }
      85             : 
      86             :   bool consumeFront(char C) {
      87             :     if (!startsWith(C))
      88             :       return false;
      89        3676 :     *this = dropFront(1);
      90             :     return true;
      91             :   }
      92             : 
      93       24355 :   bool consumeFront(StringView S) {
      94       24355 :     if (!startsWith(S))
      95             :       return false;
      96        5192 :     *this = dropFront(S.size());
      97        2596 :     return true;
      98             :   }
      99             : 
     100       10309 :   bool startsWith(char C) const { return !empty() && *begin() == C; }
     101             : 
     102       43160 :   bool startsWith(StringView Str) const {
     103       86320 :     if (Str.size() > size())
     104             :       return false;
     105             :     return std::equal(Str.begin(), Str.end(), begin());
     106             :   }
     107             : 
     108        9408 :   const char &operator[](size_t Idx) const { return *(begin() + Idx); }
     109             : 
     110           0 :   const char *begin() const { return First; }
     111           0 :   const char *end() const { return Last; }
     112       81750 :   size_t size() const { return static_cast<size_t>(Last - First); }
     113           0 :   bool empty() const { return First == Last; }
     114             : };
     115             : 
     116        1143 : inline bool operator==(const StringView &LHS, const StringView &RHS) {
     117        1344 :   return LHS.size() == RHS.size() &&
     118        1143 :          std::equal(LHS.begin(), LHS.end(), RHS.begin());
     119             : }
     120             : 
     121             : #endif

Generated by: LCOV version 1.13