File: string_statics.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (167 lines) | stat: -rw-r--r-- 6,613 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright (C) 2010 Apple Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "third_party/blink/renderer/platform/wtf/text/string_statics.h"

#include <algorithm>

#include "third_party/blink/renderer/platform/wtf/dynamic_annotations.h"
#include "third_party/blink/renderer/platform/wtf/static_constructors.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
#include "third_party/blink/renderer/platform/wtf/text/convert_to_8bit_hash_reader.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/blink/renderer/platform/wtf/text/string_impl.h"
#include "third_party/blink/renderer/platform/wtf/wtf.h"

namespace WTF {

DEFINE_GLOBAL(WTF_EXPORT, AtomicString, g_null_atom);
DEFINE_GLOBAL(WTF_EXPORT, AtomicString, g_empty_atom);
DEFINE_GLOBAL(WTF_EXPORT, AtomicString, g_star_atom);
DEFINE_GLOBAL(WTF_EXPORT, AtomicString, g_xml_atom);
DEFINE_GLOBAL(WTF_EXPORT, AtomicString, g_xmlns_atom);
DEFINE_GLOBAL(WTF_EXPORT, AtomicString, g_xlink_atom);
DEFINE_GLOBAL(WTF_EXPORT, AtomicString, g_http_atom);
DEFINE_GLOBAL(WTF_EXPORT, AtomicString, g_https_atom);

// This is not an AtomicString because it is unlikely to be used as an
// event/element/attribute name, so it shouldn't pollute the AtomicString hash
// table.
DEFINE_GLOBAL(WTF_EXPORT, String, g_xmlns_with_colon);

DEFINE_GLOBAL(WTF_EXPORT, String, g_empty_string);
DEFINE_GLOBAL(WTF_EXPORT, String, g_empty_string16_bit);

namespace {
alignas(String) char g_canonical_whitespace_table_storage[sizeof(
    NewlineThenWhitespaceStringsTable::TableType)];
}

WTF_EXPORT unsigned ComputeHashForWideString(const UChar* str,
                                             unsigned length) {
  bool is_all_latin1 = true;
  for (unsigned i = 0; i < length; ++i) {
    if (str[i] & 0xff00) {
      is_all_latin1 = false;
      break;
    }
  }
  if (is_all_latin1) {
    return StringHasher::ComputeHashAndMaskTop8Bits<ConvertTo8BitHashReader>(
        (char*)str, length);
  } else {
    return StringHasher::ComputeHashAndMaskTop8Bits((char*)str, length * 2);
  }
}

WTF_EXPORT const NewlineThenWhitespaceStringsTable::TableType&
    NewlineThenWhitespaceStringsTable::g_table_ =
        *reinterpret_cast<NewlineThenWhitespaceStringsTable::TableType*>(
            g_canonical_whitespace_table_storage);

NOINLINE unsigned StringImpl::HashSlowCase() const {
  if (Is8Bit()) {
    // This is the common case, so we take the size penalty
    // of the inlining here.
    SetHash(StringHasher::ComputeHashAndMaskTop8BitsInline((char*)Characters8(),
                                                           length_));
  } else {
    SetHash(ComputeHashForWideString(Characters16(), length_));
  }
  return ExistingHash();
}

void AtomicString::Init() {
  DCHECK(IsMainThread());

  new (NotNullTag::kNotNull, (void*)&g_null_atom) AtomicString;
  new (NotNullTag::kNotNull, (void*)&g_empty_atom) AtomicString("");
}

scoped_refptr<StringImpl> AddStaticASCIILiteral(
    base::span<const char> literal) {
  return base::AdoptRef(StringImpl::CreateStatic(literal));
}

void NewlineThenWhitespaceStringsTable::Init() {
  char whitespace_buffer[kTableSize + 1] = {'\n'};
  std::ranges::fill(base::span(whitespace_buffer).subspan<1u>(), ' ');

  // Keep g_table_[0] uninitialized.
  for (size_t length = 1; length < kTableSize; ++length) {
    auto* string_impl =
        StringImpl::CreateStatic(base::span(whitespace_buffer).first(length));
    new (NotNullTag::kNotNull, (void*)(&g_table_[length]))
        String(AtomicString(string_impl).GetString());
  }
}

bool NewlineThenWhitespaceStringsTable::IsNewlineThenWhitespaces(
    const StringView& view) {
  if (view.empty()) {
    return false;
  }
  if (view[0] != '\n') {
    return false;
  }
  if (view.Is8Bit()) {
    return std::ranges::all_of(view.Span8().subspan(1u),
                               [](LChar ch) { return ch == ' '; });
  }
  return std::ranges::all_of(view.Span16().subspan(1u),
                             [](UChar ch) { return ch == ' '; });
}

void StringStatics::Init() {
  DCHECK(IsMainThread());

  StringImpl::InitStatics();
  new (NotNullTag::kNotNull, (void*)&g_empty_string) String(StringImpl::empty_);
  new (NotNullTag::kNotNull, (void*)&g_empty_string16_bit)
      String(StringImpl::empty16_bit_);

  // FIXME: These should be allocated at compile time.
  new (NotNullTag::kNotNull, (void*)&g_star_atom) AtomicString("*");
  new (NotNullTag::kNotNull, (void*)&g_xml_atom)
      AtomicString(AddStaticASCIILiteral(base::span_from_cstring("xml")));
  new (NotNullTag::kNotNull, (void*)&g_xmlns_atom)
      AtomicString(AddStaticASCIILiteral(base::span_from_cstring("xmlns")));
  new (NotNullTag::kNotNull, (void*)&g_xlink_atom)
      AtomicString(AddStaticASCIILiteral(base::span_from_cstring("xlink")));
  new (NotNullTag::kNotNull, (void*)&g_xmlns_with_colon) String("xmlns:");
  new (NotNullTag::kNotNull, (void*)&g_http_atom)
      AtomicString(AddStaticASCIILiteral(base::span_from_cstring("http")));
  new (NotNullTag::kNotNull, (void*)&g_https_atom)
      AtomicString(AddStaticASCIILiteral(base::span_from_cstring("https")));

  NewlineThenWhitespaceStringsTable::Init();
}

}  // namespace WTF