• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "../string_util.h"
6 #include <string>
7 #include <string_view>
8 #include "fillins_string_util.h"
9 
10 
11 namespace bssl {
12 
13 namespace fillins {
14 
15 
16 // TODO(bbe): get rid of this
HexEncode(const void * bytes,size_t size)17 std::string HexEncode(const void *bytes, size_t size) {
18   return bssl::string_util::HexEncode((const uint8_t *)bytes, size);
19 }
20 
IsUnicodeWhitespace(char c)21 static bool IsUnicodeWhitespace(char c) {
22   return c == 9 || c == 10 || c == 11 || c == 12 || c == 13 || c == ' ';
23 }
24 
CollapseWhitespaceASCII(std::string_view text,bool trim_sequences_with_line_breaks)25 std::string CollapseWhitespaceASCII(std::string_view text,
26                                     bool trim_sequences_with_line_breaks) {
27   std::string result;
28   result.resize(text.size());
29 
30   // Set flags to pretend we're already in a trimmed whitespace sequence, so we
31   // will trim any leading whitespace.
32   bool in_whitespace = true;
33   bool already_trimmed = true;
34 
35   int chars_written = 0;
36   for (auto i = text.begin(); i != text.end(); ++i) {
37     if (IsUnicodeWhitespace(*i)) {
38       if (!in_whitespace) {
39         // Reduce all whitespace sequences to a single space.
40         in_whitespace = true;
41         result[chars_written++] = L' ';
42       }
43       if (trim_sequences_with_line_breaks && !already_trimmed &&
44           ((*i == '\n') || (*i == '\r'))) {
45         // Whitespace sequences containing CR or LF are eliminated entirely.
46         already_trimmed = true;
47         --chars_written;
48       }
49     } else {
50       // Non-whitespace chracters are copied straight across.
51       in_whitespace = false;
52       already_trimmed = false;
53       result[chars_written++] = *i;
54     }
55   }
56 
57   if (in_whitespace && !already_trimmed) {
58     // Any trailing whitespace is eliminated.
59     --chars_written;
60   }
61 
62   result.resize(chars_written);
63   return result;
64 }
65 
66 // TODO(bbe): get rid of this (used to be strcasecmp in google3, which
67 // causes windows pain because msvc and strings.h)
EqualsCaseInsensitiveASCII(std::string_view a,std::string_view b)68 bool EqualsCaseInsensitiveASCII(std::string_view a, std::string_view b) {
69   return bssl::string_util::IsEqualNoCase(a, b);
70 }
71 
IsAsciiAlpha(char c)72 bool IsAsciiAlpha(char c) {
73   return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
74 }
75 
IsAsciiDigit(char c)76 bool IsAsciiDigit(char c) { return c >= '0' && c <= '9'; }
77 
ReplaceSubstringsAfterOffset(std::string * s,size_t offset,std::string_view find,std::string_view replace)78 void ReplaceSubstringsAfterOffset(std::string *s, size_t offset,
79                                   std::string_view find,
80                                   std::string_view replace) {
81   std::string_view prefix(s->data(), offset);
82   std::string suffix =
83       bssl::string_util::FindAndReplace(s->substr(offset), find, replace);
84   *s = std::string(prefix) + suffix;
85 };
86 
87 }  // namespace fillins
88 
89 }  // namespace bssl
90