3
* Copyright 2004--2005, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#include "talk/base/stringutils.h"
29
#include "talk/base/common.h"
33
bool memory_check(const void* memory, int c, size_t count) {
34
const char* char_memory = static_cast<const char*>(memory);
35
char char_c = static_cast<char>(c);
36
for (size_t i = 0; i < count; ++i) {
37
if (char_memory[i] != char_c) {
44
bool string_match(const char* target, const char* pattern) {
46
if (*pattern == '*') {
51
if ((toupper(*pattern) == toupper(*target))
52
&& string_match(target + 1, pattern + 1)) {
59
if (toupper(*pattern) != toupper(*target)) {
70
int ascii_string_compare(const wchar_t* s1, const char* s2, size_t n,
71
CharacterTransformation transformation) {
74
if (n-- == 0) return 0;
75
c1 = transformation(*s1);
76
// Double check that characters are not UTF-8
77
ASSERT(static_cast<unsigned char>(*s2) < 128);
78
// Note: *s2 gets implicitly promoted to wchar_t
79
c2 = transformation(*s2);
80
if (c1 != c2) return (c1 < c2) ? -1 : 1;
87
size_t asccpyn(wchar_t* buffer, size_t buflen,
88
const char* source, size_t srclen) {
92
if (srclen == SIZE_UNKNOWN) {
93
srclen = strlenn(source, buflen - 1);
94
} else if (srclen >= buflen) {
98
// Double check that characters are not UTF-8
99
for (size_t pos = 0; pos < srclen; ++pos)
100
ASSERT(static_cast<unsigned char>(source[pos]) < 128);
102
std::copy(source, source + srclen, buffer);
109
void replace_substrs(const char *search,
115
while ((pos = s->find(search, pos, search_len)) != std::string::npos) {
116
s->replace(pos, search_len, replace, replace_len);
121
bool starts_with(const char *s1, const char *s2) {
122
return strncmp(s1, s2, strlen(s2)) == 0;
125
bool ends_with(const char *s1, const char *s2) {
126
size_t s1_length = strlen(s1);
127
size_t s2_length = strlen(s2);
129
if (s2_length > s1_length) {
133
const char* start = s1 + (s1_length - s2_length);
134
return strncmp(start, s2, s2_length) == 0;
137
static const char kWhitespace[] = " \n\r\t";
139
std::string string_trim(const std::string& s) {
140
std::string::size_type first = s.find_first_not_of(kWhitespace);
141
std::string::size_type last = s.find_last_not_of(kWhitespace);
143
if (first == std::string::npos || last == std::string::npos) {
144
return std::string("");
147
return s.substr(first, last - first + 1);
150
} // namespace talk_base