• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Useful string functions and so forth.  This is a grab-bag file.
2 //
3 // You might also want to look at memutil.h, which holds mem*()
4 // equivalents of a lot of the str*() functions in string.h,
5 // eg memstr, mempbrk, etc.
6 //
7 // These functions work fine for UTF-8 strings as long as you can
8 // consider them to be just byte strings.  For example, due to the
9 // design of UTF-8 you do not need to worry about accidental matches,
10 // as long as all your inputs are valid UTF-8 (use \uHHHH, not \xHH or \oOOO).
11 //
12 // Caveats:
13 // * all the lengths in these routines refer to byte counts,
14 //   not character counts.
15 // * case-insensitivity in these routines assumes that all the letters
16 //   in question are in the range A-Z or a-z.
17 //
18 // If you need Unicode specific processing (for example being aware of
19 // Unicode character boundaries, or knowledge of Unicode casing rules,
20 // or various forms of equivalence and normalization), take a look at
21 // files in i18n/utf8.
22 
23 #ifndef DYNAMIC_DEPTH_INTERNAL_STRINGS_UTIL_H_  // NOLINT
24 #define DYNAMIC_DEPTH_INTERNAL_STRINGS_UTIL_H_  // NOLINT
25 
26 #include <string>
27 
28 #include "base/port.h"
29 #include "fastmem.h"
30 
31 namespace dynamic_depth {
32 
33 // Returns whether str begins with prefix.
HasPrefixString(const string & str,const string & prefix)34 inline bool HasPrefixString(const string& str, const string& prefix) {
35   return str.length() >= prefix.length() &&
36          ::dynamic_depth::strings::memeq(&str[0], &prefix[0], prefix.length());
37 }
38 
39 // Returns whether str ends with suffix.
HasSuffixString(const string & str,const string & suffix)40 inline bool HasSuffixString(const string& str, const string& suffix) {
41   return str.length() >= suffix.length() &&
42          ::dynamic_depth::strings::memeq(
43              &str[0] + (str.length() - suffix.length()), &suffix[0],
44              suffix.length());
45 }
46 
47 }  // namespace dynamic_depth
48 
49 #endif  // DYNAMIC_DEPTH_INTERNAL_STRINGS_UTIL_H_  // NOLINT
50