~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/googletalk/libjingle/talk/base/stringutils.cc

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
31
 
namespace talk_base {
32
 
 
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) {
38
 
      return false;
39
 
    }
40
 
  }
41
 
  return true;
42
 
}
43
 
 
44
 
bool 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
70
 
int 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
 
 
87
 
size_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
 
 
109
 
void 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
 
 
121
 
bool starts_with(const char *s1, const char *s2) {
122
 
  return strncmp(s1, s2, strlen(s2)) == 0;
123
 
}
124
 
 
125
 
bool 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
 
 
137
 
static const char kWhitespace[] = " \n\r\t";
138
 
 
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);
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