Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Demangle/StringView.h
Line
Count
Source (jump to first uncovered line)
1
//===--- StringView.h -------------------------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// FIXME: Use std::string_view instead when we support C++17.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef DEMANGLE_STRINGVIEW_H
14
#define DEMANGLE_STRINGVIEW_H
15
16
#include "DemangleConfig.h"
17
#include <algorithm>
18
#include <cassert>
19
#include <cstring>
20
21
DEMANGLE_NAMESPACE_BEGIN
22
23
class StringView {
24
  const char *First;
25
  const char *Last;
26
27
public:
28
  static const size_t npos = ~size_t(0);
29
30
  template <size_t N>
31
  StringView(const char (&Str)[N]) : First(Str), Last(Str + N - 1) {}
32
  StringView(const char *First_, const char *Last_)
33
37.7k
      : First(First_), Last(Last_) {}
34
  StringView(const char *First_, size_t Len)
35
685
      : First(First_), Last(First_ + Len) {}
36
86.4k
  StringView(const char *Str) : First(Str), Last(Str + std::strlen(Str)) {}
37
7.37k
  StringView() : First(nullptr), Last(nullptr) {}
38
39
4
  StringView substr(size_t From) const {
40
4
    return StringView(begin() + From, size() - From);
41
4
  }
42
43
465
  size_t find(char C, size_t From = 0) const {
44
465
    size_t FindBegin = std::min(From, size());
45
465
    // Avoid calling memchr with nullptr.
46
465
    if (FindBegin < size()) {
47
459
      // Just forward to memchr, which is faster than a hand-rolled loop.
48
459
      if (const void *P = ::memchr(First + FindBegin, C, size() - FindBegin))
49
458
        return size_t(static_cast<const char *>(P) - First);
50
7
    }
51
7
    return npos;
52
7
  }
53
54
3.52k
  StringView substr(size_t From, size_t To) const {
55
3.52k
    if (To >= size())
56
3
      To = size() - 1;
57
3.52k
    if (From >= size())
58
0
      From = size() - 1;
59
3.52k
    return StringView(First + From, First + To);
60
3.52k
  }
61
62
14.1k
  StringView dropFront(size_t N = 1) const {
63
14.1k
    if (N >= size())
64
1.08k
      N = size();
65
14.1k
    return StringView(First + N, Last);
66
14.1k
  }
67
68
9
  StringView dropBack(size_t N = 1) const {
69
9
    if (N >= size())
70
0
      N = size();
71
9
    return StringView(First, Last - N);
72
9
  }
73
74
9.74k
  char front() const {
75
9.74k
    assert(!empty());
76
9.74k
    return *begin();
77
9.74k
  }
78
79
10
  char back() const {
80
10
    assert(!empty());
81
10
    return *(end() - 1);
82
10
  }
83
84
6.52k
  char popFront() {
85
6.52k
    assert(!empty());
86
6.52k
    return *First++;
87
6.52k
  }
88
89
13.5k
  bool consumeFront(char C) {
90
13.5k
    if (!startsWith(C))
91
8.00k
      return false;
92
5.56k
    *this = dropFront(1);
93
5.56k
    return true;
94
5.56k
  }
95
96
28.0k
  bool consumeFront(StringView S) {
97
28.0k
    if (!startsWith(S))
98
25.3k
      return false;
99
2.79k
    *this = dropFront(S.size());
100
2.79k
    return true;
101
2.79k
  }
102
103
19.9k
  bool startsWith(char C) const { return !empty() && 
*begin() == C19.9k
; }
104
105
55.3k
  bool startsWith(StringView Str) const {
106
55.3k
    if (Str.size() > size())
107
6.46k
      return false;
108
48.8k
    return std::equal(Str.begin(), Str.end(), begin());
109
48.8k
  }
110
111
19.0k
  const char &operator[](size_t Idx) const { return *(begin() + Idx); }
112
113
189k
  const char *begin() const { return First; }
114
51.2k
  const char *end() const { return Last; }
115
200k
  size_t size() const { return static_cast<size_t>(Last - First); }
116
34.7k
  bool empty() const { return First == Last; }
117
};
118
119
1.69k
inline bool operator==(const StringView &LHS, const StringView &RHS) {
120
1.69k
  return LHS.size() == RHS.size() &&
121
1.69k
         
std::equal(LHS.begin(), LHS.end(), RHS.begin())720
;
122
1.69k
}
123
124
DEMANGLE_NAMESPACE_END
125
126
#endif