Note: We no longer publish the latest version of our code here. We primarily use a kumc-bmi github organization. The heron ETL repository, in particular, is not public. Peers in the informatics community should see MultiSiteDev for details on requesting access.

source: webrtc/talk/base/stringutils.cc @ 0:4bda6873e34c

pub_scrub_3792 tip
Last change on this file since 0:4bda6873e34c was 0:4bda6873e34c, checked in by Michael Prittie <mprittie@…>, 6 years ago

Scrubbed password for publication.

File size: 4.5 KB
Line 
1/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
28#include "talk/base/stringutils.h"
29#include "talk/base/common.h"
30
31namespace talk_base {
32
33bool 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) {
38      return false;
39    }
40  }
41  return true;
42}
43
44bool string_match(const char* target, const char* pattern) {
45  while (*pattern) {
46    if (*pattern == '*') {
47      if (!*++pattern) {
48        return true;
49      }
50      while (*target) {
51        if ((toupper(*pattern) == toupper(*target))
52            && string_match(target + 1, pattern + 1)) {
53          return true;
54        }
55        ++target;
56      }
57      return false;
58    } else {
59      if (toupper(*pattern) != toupper(*target)) {
60        return false;
61      }
62      ++target;
63      ++pattern;
64    }
65  }
66  return !*target;
67}
68
69#ifdef WIN32
70int ascii_string_compare(const wchar_t* s1, const char* s2, size_t n,
71                         CharacterTransformation transformation) {
72  wchar_t c1, c2;
73  while (true) {
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;
81    if (!c1) return 0;
82    ++s1;
83    ++s2;
84  }
85}
86
87size_t asccpyn(wchar_t* buffer, size_t buflen,
88               const char* source, size_t srclen) {
89  if (buflen <= 0)
90    return 0;
91
92  if (srclen == SIZE_UNKNOWN) {
93    srclen = strlenn(source, buflen - 1);
94  } else if (srclen >= buflen) {
95    srclen = buflen - 1;
96  }
97#if _DEBUG
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);
101#endif  // _DEBUG
102  std::copy(source, source + srclen, buffer);
103  buffer[srclen] = 0;
104  return srclen;
105}
106
107#endif  // WIN32
108
109void replace_substrs(const char *search,
110                     size_t search_len,
111                     const char *replace,
112                     size_t replace_len,
113                     std::string *s) {
114  size_t pos = 0;
115  while ((pos = s->find(search, pos, search_len)) != std::string::npos) {
116    s->replace(pos, search_len, replace, replace_len);
117    pos += replace_len;
118  }
119}
120
121bool starts_with(const char *s1, const char *s2) {
122  return strncmp(s1, s2, strlen(s2)) == 0;
123}
124
125bool ends_with(const char *s1, const char *s2) {
126  size_t s1_length = strlen(s1);
127  size_t s2_length = strlen(s2);
128
129  if (s2_length > s1_length) {
130    return false;
131  }
132
133  const char* start = s1 + (s1_length - s2_length);
134  return strncmp(start, s2, s2_length) == 0;
135}
136
137static const char kWhitespace[] = " \n\r\t";
138
139std::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);
142
143  if (first == std::string::npos || last == std::string::npos) {
144    return std::string("");
145  }
146
147  return s.substr(first, last - first + 1);
148}
149
150}  // namespace talk_base
Note: See TracBrowser for help on using the repository browser.