Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / installer / mini_installer / mini_string.h
blob23e53dc2b1b6fdf3e5c54f457efe1df811d56ce0
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_INSTALLER_MINI_INSTALLER_MINI_STRING_H_
6 #define CHROME_INSTALLER_MINI_INSTALLER_MINI_STRING_H_
8 #ifndef COMPILE_ASSERT
9 // Some bots that build mini_installer don't know static_assert.
10 #if __cplusplus >= 201103L
11 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg)
12 #else
13 template <bool>
14 struct CompileAssert {};
15 #define COMPILE_ASSERT(expr, msg) \
16 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
17 #endif
18 #endif
20 namespace mini_installer {
22 // NOTE: Do not assume that these string functions support UTF encoding.
23 // This is fine for the purposes of the mini_installer, but you have
24 // been warned!
26 // Formats a sequence of |bytes| as hex. The |str| buffer must have room for
27 // at least 2*|size| + 1.
28 bool HexEncode(const void* bytes, size_t size, wchar_t* str, size_t str_size);
30 // Counts the number of characters in the string up to a maximum of
31 // alloc_size. The highest return value from this function can therefore be
32 // alloc_size - 1 since |alloc_size| includes the \0 terminator.
33 size_t SafeStrLen(const wchar_t* str, size_t alloc_size);
35 // Simple replacement for CRT string copy method that does not overflow.
36 // Returns true if the source was copied successfully otherwise returns false.
37 // Parameter src is assumed to be NULL terminated and the NULL character is
38 // copied over to string dest.
39 bool SafeStrCopy(wchar_t* dest, size_t dest_size, const wchar_t* src);
41 // Simple replacement for CRT string copy method that does not overflow.
42 // Returns true if the source was copied successfully otherwise returns false.
43 // Parameter src is assumed to be NULL terminated and the NULL character is
44 // copied over to string dest. If the return value is false, the |dest|
45 // string should be the same as it was before.
46 bool SafeStrCat(wchar_t* dest, size_t dest_size, const wchar_t* src);
48 // Function to check if a string (specified by str) ends with another string
49 // (specified by end_str).
50 bool StrEndsWith(const wchar_t* str, const wchar_t* end_str);
52 // Function to check if a string (specified by str) starts with another string
53 // (specified by start_str).
54 bool StrStartsWith(const wchar_t* str, const wchar_t* start_str);
56 // Case insensitive search of the first occurrence of |find| in |source|.
57 const wchar_t* SearchStringI(const wchar_t* source, const wchar_t* find);
59 // Searches for |tag| within |str|. Returns true if |tag| is found and is
60 // immediately followed by '-' or is at the end of the string. If |position|
61 // is non-NULL, the location of the tag is returned in |*position| on success.
62 bool FindTagInStr(const wchar_t* str, const wchar_t* tag,
63 const wchar_t** position);
65 // Takes the path to file and returns a pointer to the basename component.
66 // Example input -> output:
67 // c:\full\path\to\file.ext -> file.ext
68 // file.ext -> file.ext
69 // Note: |size| is the number of characters in |path| not including the string
70 // terminator.
71 const wchar_t* GetNameFromPathExt(const wchar_t* path, size_t size);
72 wchar_t* GetNameFromPathExt(wchar_t* path, size_t size);
74 // A string class that manages a fixed size buffer on the stack.
75 // The methods in the class are based on the above string methods and the
76 // class additionally is careful about proper buffer termination.
77 template <size_t kCapacity>
78 class StackString {
79 public:
80 StackString() {
81 COMPILE_ASSERT(kCapacity != 0, invalid_buffer_size);
82 buffer_[kCapacity] = L'\0'; // We always reserve 1 more than asked for.
83 clear();
86 // We do not expose a constructor that accepts a string pointer on purpose.
87 // We expect the caller to call assign() and handle failures.
89 // Returns the number of reserved characters in this buffer, _including_
90 // the reserved char for the terminator.
91 size_t capacity() const {
92 return kCapacity;
95 wchar_t* get() {
96 return buffer_;
99 bool assign(const wchar_t* str) {
100 return SafeStrCopy(buffer_, kCapacity, str);
103 bool append(const wchar_t* str) {
104 return SafeStrCat(buffer_, kCapacity, str);
107 void clear() {
108 buffer_[0] = L'\0';
111 size_t length() const {
112 return SafeStrLen(buffer_, kCapacity);
115 // Does a case insensitive search for a substring.
116 const wchar_t* findi(const wchar_t* find) const {
117 return SearchStringI(buffer_, find);
120 // Case insensitive string compare.
121 int comparei(const wchar_t* str) const {
122 return lstrcmpiW(buffer_, str);
125 // Case sensitive string compare.
126 int compare(const wchar_t* str) const {
127 return lstrcmpW(buffer_, str);
130 // Terminates the string at the specified location.
131 // Note: this method has no effect if this object's length is less than
132 // |location|.
133 bool truncate_at(size_t location) {
134 if (location >= kCapacity)
135 return false;
136 buffer_[location] = L'\0';
137 return true;
140 protected:
141 // We reserve 1 more than what is asked for as a safeguard against
142 // off-by-one errors.
143 wchar_t buffer_[kCapacity + 1];
145 private:
146 StackString(const StackString&);
147 StackString& operator=(const StackString&);
150 } // namespace mini_installer
152 #endif // CHROME_INSTALLER_MINI_INSTALLER_MINI_STRING_H_