henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
Jonas Olsson | 7439534 | 2018-04-03 12:22:07 +0200 | [diff] [blame] | 10 | #include <algorithm> |
| 11 | #include <cstdio> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/stringutils.h" |
| 14 | #include "rtc_base/checks.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | |
| 16 | namespace rtc { |
| 17 | |
| 18 | bool memory_check(const void* memory, int c, size_t count) { |
| 19 | const char* char_memory = static_cast<const char*>(memory); |
| 20 | char char_c = static_cast<char>(c); |
| 21 | for (size_t i = 0; i < count; ++i) { |
| 22 | if (char_memory[i] != char_c) { |
| 23 | return false; |
| 24 | } |
| 25 | } |
| 26 | return true; |
| 27 | } |
| 28 | |
| 29 | bool string_match(const char* target, const char* pattern) { |
| 30 | while (*pattern) { |
| 31 | if (*pattern == '*') { |
| 32 | if (!*++pattern) { |
| 33 | return true; |
| 34 | } |
| 35 | while (*target) { |
| 36 | if ((toupper(*pattern) == toupper(*target)) |
| 37 | && string_match(target + 1, pattern + 1)) { |
| 38 | return true; |
| 39 | } |
| 40 | ++target; |
| 41 | } |
| 42 | return false; |
| 43 | } else { |
| 44 | if (toupper(*pattern) != toupper(*target)) { |
| 45 | return false; |
| 46 | } |
| 47 | ++target; |
| 48 | ++pattern; |
| 49 | } |
| 50 | } |
| 51 | return !*target; |
| 52 | } |
| 53 | |
| 54 | #if defined(WEBRTC_WIN) |
| 55 | int ascii_string_compare(const wchar_t* s1, const char* s2, size_t n, |
| 56 | CharacterTransformation transformation) { |
| 57 | wchar_t c1, c2; |
| 58 | while (true) { |
| 59 | if (n-- == 0) return 0; |
| 60 | c1 = transformation(*s1); |
| 61 | // Double check that characters are not UTF-8 |
kwiberg | 352444f | 2016-11-28 15:58:53 -0800 | [diff] [blame] | 62 | RTC_DCHECK_LT(*s2, 128); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 63 | // Note: *s2 gets implicitly promoted to wchar_t |
| 64 | c2 = transformation(*s2); |
| 65 | if (c1 != c2) return (c1 < c2) ? -1 : 1; |
| 66 | if (!c1) return 0; |
| 67 | ++s1; |
| 68 | ++s2; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | size_t asccpyn(wchar_t* buffer, size_t buflen, |
| 73 | const char* source, size_t srclen) { |
| 74 | if (buflen <= 0) |
| 75 | return 0; |
| 76 | |
| 77 | if (srclen == SIZE_UNKNOWN) { |
| 78 | srclen = strlenn(source, buflen - 1); |
| 79 | } else if (srclen >= buflen) { |
| 80 | srclen = buflen - 1; |
| 81 | } |
kwiberg | 5377bc7 | 2016-10-04 13:46:56 -0700 | [diff] [blame] | 82 | #if RTC_DCHECK_IS_ON |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 83 | // Double check that characters are not UTF-8 |
| 84 | for (size_t pos = 0; pos < srclen; ++pos) |
kwiberg | 352444f | 2016-11-28 15:58:53 -0800 | [diff] [blame] | 85 | RTC_DCHECK_LT(source[pos], 128); |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 86 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 87 | std::copy(source, source + srclen, buffer); |
| 88 | buffer[srclen] = 0; |
| 89 | return srclen; |
| 90 | } |
| 91 | |
andrew@webrtc.org | 6ae5a6d | 2014-09-16 01:03:29 +0000 | [diff] [blame] | 92 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | |
| 94 | void replace_substrs(const char *search, |
| 95 | size_t search_len, |
| 96 | const char *replace, |
| 97 | size_t replace_len, |
| 98 | std::string *s) { |
| 99 | size_t pos = 0; |
| 100 | while ((pos = s->find(search, pos, search_len)) != std::string::npos) { |
| 101 | s->replace(pos, search_len, replace, replace_len); |
| 102 | pos += replace_len; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | bool starts_with(const char *s1, const char *s2) { |
| 107 | return strncmp(s1, s2, strlen(s2)) == 0; |
| 108 | } |
| 109 | |
| 110 | bool ends_with(const char *s1, const char *s2) { |
| 111 | size_t s1_length = strlen(s1); |
| 112 | size_t s2_length = strlen(s2); |
| 113 | |
| 114 | if (s2_length > s1_length) { |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | const char* start = s1 + (s1_length - s2_length); |
| 119 | return strncmp(start, s2, s2_length) == 0; |
| 120 | } |
| 121 | |
| 122 | static const char kWhitespace[] = " \n\r\t"; |
| 123 | |
| 124 | std::string string_trim(const std::string& s) { |
| 125 | std::string::size_type first = s.find_first_not_of(kWhitespace); |
| 126 | std::string::size_type last = s.find_last_not_of(kWhitespace); |
| 127 | |
| 128 | if (first == std::string::npos || last == std::string::npos) { |
| 129 | return std::string(""); |
| 130 | } |
| 131 | |
| 132 | return s.substr(first, last - first + 1); |
| 133 | } |
| 134 | |
Jonas Olsson | 7439534 | 2018-04-03 12:22:07 +0200 | [diff] [blame] | 135 | std::string ToHex(const int i) { |
| 136 | char buffer[50]; |
| 137 | snprintf(buffer, sizeof(buffer), "%x", i); |
| 138 | |
| 139 | return std::string(buffer); |
| 140 | } |
| 141 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 142 | } // namespace rtc |