~ubuntu-branches/ubuntu/wily/clamav/wily-proposed

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/include/llvm/ADT/StringRef.h

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman, Sebastian Andrzej Siewior, Andreas Cadhalpun, Scott Kitterman, Javier Fernández-Sanguino
  • Date: 2015-01-28 00:25:13 UTC
  • mfrom: (0.48.14 sid)
  • Revision ID: package-import@ubuntu.com-20150128002513-lil2oi74cooy4lzr
Tags: 0.98.6+dfsg-1
[ Sebastian Andrzej Siewior ]
* update "fix-ssize_t-size_t-off_t-printf-modifier", include of misc.h was
  missing but was pulled in via the systemd patch.
* Don't leak return codes from libmspack to clamav API. (Closes: #774686).

[ Andreas Cadhalpun ]
* Add patch to avoid emitting incremental progress messages when not
  outputting to a terminal. (Closes: #767350)
* Update lintian-overrides for unused-file-paragraph-in-dep5-copyright.
* clamav-base.postinst: always chown /var/log/clamav and /var/lib/clamav
  to clamav:clamav, not only on fresh installations. (Closes: #775400)
* Adapt the clamav-daemon and clamav-freshclam logrotate scripts,
  so that they correctly work under systemd.
* Move the PidFile variable from the clamd/freshclam configuration files
  to the init scripts. This makes the init scripts more robust against
  misconfiguration and avoids error messages with systemd. (Closes: #767353)
* debian/copyright: drop files from Files-Excluded only present in github
  tarballs
* Drop Workaround-a-bug-in-libc-on-Hurd.patch, because hurd got fixed.
  (see #752237)
* debian/rules: Remove useless --with-system-tommath --without-included-ltdl
  configure options.

[ Scott Kitterman ]
* Stop stripping llvm when repacking the tarball as the system llvm on some
  releases is too old to use
* New upstream bugfix release
  - Library shared object revisions.
  - Includes a patch from Sebastian Andrzej Siewior making ClamAV pid files
    compatible with systemd.
  - Fix a heap out of bounds condition with crafted Yoda's crypter files.
    This issue was discovered by Felix Groebert of the Google Security Team.
  - Fix a heap out of bounds condition with crafted mew packer files. This
    issue was discovered by Felix Groebert of the Google Security Team.
  - Fix a heap out of bounds condition with crafted upx packer files. This
    issue was discovered by Kevin Szkudlapski of Quarkslab.
  - Fix a heap out of bounds condition with crafted upack packer files. This
    issue was discovered by Sebastian Andrzej Siewior. CVE-2014-9328.
  - Compensate a crash due to incorrect compiler optimization when handling
    crafted petite packer files. This issue was discovered by Sebastian
    Andrzej Siewior.
* Update lintian override for embedded zlib to match new so version

[ Javier Fernández-Sanguino ]
* Updated Spanish Debconf template translation (Closes: #773563)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===--- StringRef.h - Constant String Reference Wrapper --------*- 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
 
 
10
#ifndef LLVM_ADT_STRINGREF_H
 
11
#define LLVM_ADT_STRINGREF_H
 
12
 
 
13
#include <cassert>
 
14
#include <cstring>
 
15
#include <utility>
 
16
#include <string>
 
17
 
 
18
namespace llvm {
 
19
  template<typename T>
 
20
  class SmallVectorImpl;
 
21
  class APInt;
 
22
 
 
23
  /// StringRef - Represent a constant reference to a string, i.e. a character
 
24
  /// array and a length, which need not be null terminated.
 
25
  ///
 
26
  /// This class does not own the string data, it is expected to be used in
 
27
  /// situations where the character data resides in some other buffer, whose
 
28
  /// lifetime extends past that of the StringRef. For this reason, it is not in
 
29
  /// general safe to store a StringRef.
 
30
  class StringRef {
 
31
  public:
 
32
    typedef const char *iterator;
 
33
    typedef const char *const_iterator;
 
34
    static const size_t npos = ~size_t(0);
 
35
    typedef size_t size_type;
 
36
 
 
37
  private:
 
38
    /// The start of the string, in an external buffer.
 
39
    const char *Data;
 
40
 
 
41
    /// The length of the string.
 
42
    size_t Length;
 
43
 
 
44
    // Workaround PR5482: nearly all gcc 4.x miscompile StringRef and std::min()
 
45
    // Changing the arg of min to be an integer, instead of a reference to an
 
46
    // integer works around this bug.
 
47
    static size_t min(size_t a, size_t b) { return a < b ? a : b; }
 
48
    static size_t max(size_t a, size_t b) { return a > b ? a : b; }
 
49
 
 
50
  public:
 
51
    /// @name Constructors
 
52
    /// @{
 
53
 
 
54
    /// Construct an empty string ref.
 
55
    /*implicit*/ StringRef() : Data(0), Length(0) {}
 
56
 
 
57
    /// Construct a string ref from a cstring.
 
58
    /*implicit*/ StringRef(const char *Str)
 
59
      : Data(Str), Length(::strlen(Str)) {}
 
60
 
 
61
    /// Construct a string ref from a pointer and length.
 
62
    /*implicit*/ StringRef(const char *data, size_t length)
 
63
      : Data(data), Length(length) {}
 
64
 
 
65
    /// Construct a string ref from an std::string.
 
66
    /*implicit*/ StringRef(const std::string &Str)
 
67
      : Data(Str.data()), Length(Str.length()) {}
 
68
 
 
69
    /// @}
 
70
    /// @name Iterators
 
71
    /// @{
 
72
 
 
73
    iterator begin() const { return Data; }
 
74
 
 
75
    iterator end() const { return Data + Length; }
 
76
 
 
77
    /// @}
 
78
    /// @name String Operations
 
79
    /// @{
 
80
 
 
81
    /// data - Get a pointer to the start of the string (which may not be null
 
82
    /// terminated).
 
83
    const char *data() const { return Data; }
 
84
 
 
85
    /// empty - Check if the string is empty.
 
86
    bool empty() const { return Length == 0; }
 
87
 
 
88
    /// size - Get the string size.
 
89
    size_t size() const { return Length; }
 
90
 
 
91
    /// front - Get the first character in the string.
 
92
    char front() const {
 
93
      assert(!empty());
 
94
      return Data[0];
 
95
    }
 
96
 
 
97
    /// back - Get the last character in the string.
 
98
    char back() const {
 
99
      assert(!empty());
 
100
      return Data[Length-1];
 
101
    }
 
102
 
 
103
    /// equals - Check for string equality, this is more efficient than
 
104
    /// compare() when the relative ordering of inequal strings isn't needed.
 
105
    bool equals(StringRef RHS) const {
 
106
      return (Length == RHS.Length &&
 
107
              memcmp(Data, RHS.Data, RHS.Length) == 0);
 
108
    }
 
109
 
 
110
    /// equals_lower - Check for string equality, ignoring case.
 
111
    bool equals_lower(StringRef RHS) const {
 
112
      return Length == RHS.Length && compare_lower(RHS) == 0;
 
113
    }
 
114
 
 
115
    /// compare - Compare two strings; the result is -1, 0, or 1 if this string
 
116
    /// is lexicographically less than, equal to, or greater than the \arg RHS.
 
117
    int compare(StringRef RHS) const {
 
118
      // Check the prefix for a mismatch.
 
119
      if (int Res = memcmp(Data, RHS.Data, min(Length, RHS.Length)))
 
120
        return Res < 0 ? -1 : 1;
 
121
 
 
122
      // Otherwise the prefixes match, so we only need to check the lengths.
 
123
      if (Length == RHS.Length)
 
124
        return 0;
 
125
      return Length < RHS.Length ? -1 : 1;
 
126
    }
 
127
 
 
128
    /// compare_lower - Compare two strings, ignoring case.
 
129
    int compare_lower(StringRef RHS) const;
 
130
 
 
131
    /// compare_numeric - Compare two strings, treating sequences of digits as
 
132
    /// numbers.
 
133
    int compare_numeric(StringRef RHS) const;
 
134
 
 
135
    /// \brief Determine the edit distance between this string and another 
 
136
    /// string.
 
137
    ///
 
138
    /// \param Other the string to compare this string against.
 
139
    ///
 
140
    /// \param AllowReplacements whether to allow character
 
141
    /// replacements (change one character into another) as a single
 
142
    /// operation, rather than as two operations (an insertion and a
 
143
    /// removal).
 
144
    ///
 
145
    /// \returns the minimum number of character insertions, removals,
 
146
    /// or (if \p AllowReplacements is \c true) replacements needed to
 
147
    /// transform one of the given strings into the other. If zero,
 
148
    /// the strings are identical.
 
149
    unsigned edit_distance(StringRef Other, bool AllowReplacements = true);
 
150
 
 
151
    /// str - Get the contents as an std::string.
 
152
    std::string str() const {
 
153
      if (Data == 0) return std::string();
 
154
      return std::string(Data, Length);
 
155
    }
 
156
 
 
157
    /// @}
 
158
    /// @name Operator Overloads
 
159
    /// @{
 
160
 
 
161
    char operator[](size_t Index) const {
 
162
      assert(Index < Length && "Invalid index!");
 
163
      return Data[Index];
 
164
    }
 
165
 
 
166
    /// @}
 
167
    /// @name Type Conversions
 
168
    /// @{
 
169
 
 
170
    operator std::string() const {
 
171
      return str();
 
172
    }
 
173
 
 
174
    /// @}
 
175
    /// @name String Predicates
 
176
    /// @{
 
177
 
 
178
    /// startswith - Check if this string starts with the given \arg Prefix.
 
179
    bool startswith(StringRef Prefix) const {
 
180
      return Length >= Prefix.Length &&
 
181
             memcmp(Data, Prefix.Data, Prefix.Length) == 0;
 
182
    }
 
183
 
 
184
    /// endswith - Check if this string ends with the given \arg Suffix.
 
185
    bool endswith(StringRef Suffix) const {
 
186
      return Length >= Suffix.Length &&
 
187
             memcmp(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
 
188
    }
 
189
 
 
190
    /// @}
 
191
    /// @name String Searching
 
192
    /// @{
 
193
 
 
194
    /// find - Search for the first character \arg C in the string.
 
195
    ///
 
196
    /// \return - The index of the first occurrence of \arg C, or npos if not
 
197
    /// found.
 
198
    size_t find(char C, size_t From = 0) const {
 
199
      for (size_t i = min(From, Length), e = Length; i != e; ++i)
 
200
        if (Data[i] == C)
 
201
          return i;
 
202
      return npos;
 
203
    }
 
204
 
 
205
    /// find - Search for the first string \arg Str in the string.
 
206
    ///
 
207
    /// \return - The index of the first occurrence of \arg Str, or npos if not
 
208
    /// found.
 
209
    size_t find(StringRef Str, size_t From = 0) const;
 
210
 
 
211
    /// rfind - Search for the last character \arg C in the string.
 
212
    ///
 
213
    /// \return - The index of the last occurrence of \arg C, or npos if not
 
214
    /// found.
 
215
    size_t rfind(char C, size_t From = npos) const {
 
216
      From = min(From, Length);
 
217
      size_t i = From;
 
218
      while (i != 0) {
 
219
        --i;
 
220
        if (Data[i] == C)
 
221
          return i;
 
222
      }
 
223
      return npos;
 
224
    }
 
225
 
 
226
    /// rfind - Search for the last string \arg Str in the string.
 
227
    ///
 
228
    /// \return - The index of the last occurrence of \arg Str, or npos if not
 
229
    /// found.
 
230
    size_t rfind(StringRef Str) const;
 
231
 
 
232
    /// find_first_of - Find the first character in the string that is \arg C,
 
233
    /// or npos if not found. Same as find.
 
234
    size_type find_first_of(char C, size_t From = 0) const {
 
235
      return find(C, From);
 
236
    }
 
237
 
 
238
    /// find_first_of - Find the first character in the string that is in \arg
 
239
    /// Chars, or npos if not found.
 
240
    ///
 
241
    /// Note: O(size() + Chars.size())
 
242
    size_type find_first_of(StringRef Chars, size_t From = 0) const;
 
243
 
 
244
    /// find_first_not_of - Find the first character in the string that is not
 
245
    /// \arg C or npos if not found.
 
246
    size_type find_first_not_of(char C, size_t From = 0) const;
 
247
 
 
248
    /// find_first_not_of - Find the first character in the string that is not
 
249
    /// in the string \arg Chars, or npos if not found.
 
250
    ///
 
251
    /// Note: O(size() + Chars.size())
 
252
    size_type find_first_not_of(StringRef Chars, size_t From = 0) const;
 
253
 
 
254
    /// @}
 
255
    /// @name Helpful Algorithms
 
256
    /// @{
 
257
 
 
258
    /// count - Return the number of occurrences of \arg C in the string.
 
259
    size_t count(char C) const {
 
260
      size_t Count = 0;
 
261
      for (size_t i = 0, e = Length; i != e; ++i)
 
262
        if (Data[i] == C)
 
263
          ++Count;
 
264
      return Count;
 
265
    }
 
266
 
 
267
    /// count - Return the number of non-overlapped occurrences of \arg Str in
 
268
    /// the string.
 
269
    size_t count(StringRef Str) const;
 
270
 
 
271
    /// getAsInteger - Parse the current string as an integer of the specified
 
272
    /// radix.  If Radix is specified as zero, this does radix autosensing using
 
273
    /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
 
274
    ///
 
275
    /// If the string is invalid or if only a subset of the string is valid,
 
276
    /// this returns true to signify the error.  The string is considered
 
277
    /// erroneous if empty.
 
278
    ///
 
279
    bool getAsInteger(unsigned Radix, long long &Result) const;
 
280
    bool getAsInteger(unsigned Radix, unsigned long long &Result) const;
 
281
    bool getAsInteger(unsigned Radix, int &Result) const;
 
282
    bool getAsInteger(unsigned Radix, unsigned &Result) const;
 
283
 
 
284
    // TODO: Provide overloads for int/unsigned that check for overflow.
 
285
 
 
286
    /// getAsInteger - Parse the current string as an integer of the
 
287
    /// specified radix, or of an autosensed radix if the radix given
 
288
    /// is 0.  The current value in Result is discarded, and the
 
289
    /// storage is changed to be wide enough to store the parsed
 
290
    /// integer.
 
291
    ///
 
292
    /// Returns true if the string does not solely consist of a valid
 
293
    /// non-empty number in the appropriate base.
 
294
    ///
 
295
    /// APInt::fromString is superficially similar but assumes the
 
296
    /// string is well-formed in the given radix.
 
297
    bool getAsInteger(unsigned Radix, APInt &Result) const;
 
298
 
 
299
    /// @}
 
300
    /// @name Substring Operations
 
301
    /// @{
 
302
 
 
303
    /// substr - Return a reference to the substring from [Start, Start + N).
 
304
    ///
 
305
    /// \param Start - The index of the starting character in the substring; if
 
306
    /// the index is npos or greater than the length of the string then the
 
307
    /// empty substring will be returned.
 
308
    ///
 
309
    /// \param N - The number of characters to included in the substring. If N
 
310
    /// exceeds the number of characters remaining in the string, the string
 
311
    /// suffix (starting with \arg Start) will be returned.
 
312
    StringRef substr(size_t Start, size_t N = npos) const {
 
313
      Start = min(Start, Length);
 
314
      return StringRef(Data + Start, min(N, Length - Start));
 
315
    }
 
316
 
 
317
    /// slice - Return a reference to the substring from [Start, End).
 
318
    ///
 
319
    /// \param Start - The index of the starting character in the substring; if
 
320
    /// the index is npos or greater than the length of the string then the
 
321
    /// empty substring will be returned.
 
322
    ///
 
323
    /// \param End - The index following the last character to include in the
 
324
    /// substring. If this is npos, or less than \arg Start, or exceeds the
 
325
    /// number of characters remaining in the string, the string suffix
 
326
    /// (starting with \arg Start) will be returned.
 
327
    StringRef slice(size_t Start, size_t End) const {
 
328
      Start = min(Start, Length);
 
329
      End = min(max(Start, End), Length);
 
330
      return StringRef(Data + Start, End - Start);
 
331
    }
 
332
 
 
333
    /// split - Split into two substrings around the first occurrence of a
 
334
    /// separator character.
 
335
    ///
 
336
    /// If \arg Separator is in the string, then the result is a pair (LHS, RHS)
 
337
    /// such that (*this == LHS + Separator + RHS) is true and RHS is
 
338
    /// maximal. If \arg Separator is not in the string, then the result is a
 
339
    /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
 
340
    ///
 
341
    /// \param Separator - The character to split on.
 
342
    /// \return - The split substrings.
 
343
    std::pair<StringRef, StringRef> split(char Separator) const {
 
344
      size_t Idx = find(Separator);
 
345
      if (Idx == npos)
 
346
        return std::make_pair(*this, StringRef());
 
347
      return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
 
348
    }
 
349
 
 
350
    /// split - Split into two substrings around the first occurrence of a
 
351
    /// separator string.
 
352
    ///
 
353
    /// If \arg Separator is in the string, then the result is a pair (LHS, RHS)
 
354
    /// such that (*this == LHS + Separator + RHS) is true and RHS is
 
355
    /// maximal. If \arg Separator is not in the string, then the result is a
 
356
    /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
 
357
    ///
 
358
    /// \param Separator - The string to split on.
 
359
    /// \return - The split substrings.
 
360
    std::pair<StringRef, StringRef> split(StringRef Separator) const {
 
361
      size_t Idx = find(Separator);
 
362
      if (Idx == npos)
 
363
        return std::make_pair(*this, StringRef());
 
364
      return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
 
365
    }
 
366
 
 
367
    /// split - Split into substrings around the occurrences of a separator
 
368
    /// string.
 
369
    ///
 
370
    /// Each substring is stored in \arg A. If \arg MaxSplit is >= 0, at most
 
371
    /// \arg MaxSplit splits are done and consequently <= \arg MaxSplit
 
372
    /// elements are added to A.
 
373
    /// If \arg KeepEmpty is false, empty strings are not added to \arg A. They
 
374
    /// still count when considering \arg MaxSplit
 
375
    /// An useful invariant is that
 
376
    /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
 
377
    ///
 
378
    /// \param A - Where to put the substrings.
 
379
    /// \param Separator - The string to split on.
 
380
    /// \param MaxSplit - The maximum number of times the string is split.
 
381
    /// \param KeepEmpty - True if empty substring should be added.
 
382
    void split(SmallVectorImpl<StringRef> &A,
 
383
               StringRef Separator, int MaxSplit = -1,
 
384
               bool KeepEmpty = true) const;
 
385
 
 
386
    /// rsplit - Split into two substrings around the last occurrence of a
 
387
    /// separator character.
 
388
    ///
 
389
    /// If \arg Separator is in the string, then the result is a pair (LHS, RHS)
 
390
    /// such that (*this == LHS + Separator + RHS) is true and RHS is
 
391
    /// minimal. If \arg Separator is not in the string, then the result is a
 
392
    /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
 
393
    ///
 
394
    /// \param Separator - The character to split on.
 
395
    /// \return - The split substrings.
 
396
    std::pair<StringRef, StringRef> rsplit(char Separator) const {
 
397
      size_t Idx = rfind(Separator);
 
398
      if (Idx == npos)
 
399
        return std::make_pair(*this, StringRef());
 
400
      return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
 
401
    }
 
402
 
 
403
    /// @}
 
404
  };
 
405
 
 
406
  /// @name StringRef Comparison Operators
 
407
  /// @{
 
408
 
 
409
  inline bool operator==(StringRef LHS, StringRef RHS) {
 
410
    return LHS.equals(RHS);
 
411
  }
 
412
 
 
413
  inline bool operator!=(StringRef LHS, StringRef RHS) {
 
414
    return !(LHS == RHS);
 
415
  }
 
416
 
 
417
  inline bool operator<(StringRef LHS, StringRef RHS) {
 
418
    return LHS.compare(RHS) == -1;
 
419
  }
 
420
 
 
421
  inline bool operator<=(StringRef LHS, StringRef RHS) {
 
422
    return LHS.compare(RHS) != 1;
 
423
  }
 
424
 
 
425
  inline bool operator>(StringRef LHS, StringRef RHS) {
 
426
    return LHS.compare(RHS) == 1;
 
427
  }
 
428
 
 
429
  inline bool operator>=(StringRef LHS, StringRef RHS) {
 
430
    return LHS.compare(RHS) != -1;
 
431
  }
 
432
 
 
433
  /// @}
 
434
 
 
435
}
 
436
 
 
437
#endif