Serenity Operating System
at hosted 216 lines 6.8 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 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, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/ByteBuffer.h> 28#include <AK/Memory.h> 29#include <AK/String.h> 30#include <AK/StringView.h> 31#include <AK/FlyString.h> 32#include <AK/Vector.h> 33 34namespace AK { 35 36StringView::StringView(const String& string) 37 : m_impl(string.impl()) 38 , m_characters(string.characters()) 39 , m_length(string.length()) 40{ 41} 42 43StringView::StringView(const FlyString& string) 44 : m_impl(string.impl()) 45 , m_characters(string.characters()) 46 , m_length(string.length()) 47{ 48} 49 50StringView::StringView(const ByteBuffer& buffer) 51 : m_characters((const char*)buffer.data()) 52 , m_length(buffer.size()) 53{ 54} 55 56Vector<StringView> StringView::split_view(const char separator, bool keep_empty) const 57{ 58 if (is_empty()) 59 return {}; 60 61 Vector<StringView> v; 62 size_t substart = 0; 63 for (size_t i = 0; i < length(); ++i) { 64 char ch = characters_without_null_termination()[i]; 65 if (ch == separator) { 66 size_t sublen = i - substart; 67 if (sublen != 0 || keep_empty) 68 v.append(substring_view(substart, sublen)); 69 substart = i + 1; 70 } 71 } 72 size_t taillen = length() - substart; 73 if (taillen != 0 || keep_empty) 74 v.append(substring_view(substart, taillen)); 75 return v; 76} 77 78Vector<StringView> StringView::lines(bool consider_cr) const 79{ 80 if (is_empty()) 81 return {}; 82 83 if (!consider_cr) 84 return split_view('\n', true); 85 86 Vector<StringView> v; 87 size_t substart = 0; 88 bool last_ch_was_cr = false; 89 bool split_view = false; 90 for (size_t i = 0; i < length(); ++i) { 91 char ch = characters_without_null_termination()[i]; 92 if (ch == '\n') { 93 split_view = true; 94 if (last_ch_was_cr) { 95 substart = i + 1; 96 split_view = false; 97 last_ch_was_cr = false; 98 } 99 } 100 if (ch == '\r') { 101 split_view = true; 102 last_ch_was_cr = true; 103 } 104 if (split_view) { 105 size_t sublen = i - substart; 106 v.append(substring_view(substart, sublen)); 107 substart = i + 1; 108 } 109 split_view = false; 110 } 111 size_t taillen = length() - substart; 112 if (taillen != 0) 113 v.append(substring_view(substart, taillen)); 114 return v; 115} 116 117bool StringView::starts_with(char ch) const 118{ 119 if (is_empty()) 120 return false; 121 return ch == characters_without_null_termination()[0]; 122} 123 124bool StringView::starts_with(const StringView& str) const 125{ 126 if (str.is_empty()) 127 return true; 128 if (is_empty()) 129 return false; 130 if (str.length() > length()) 131 return false; 132 if (characters_without_null_termination() == str.characters_without_null_termination()) 133 return true; 134 return !memcmp(characters_without_null_termination(), str.characters_without_null_termination(), str.length()); 135} 136 137bool StringView::ends_with(char ch) const 138{ 139 if (is_empty()) 140 return false; 141 return ch == characters_without_null_termination()[length() - 1]; 142} 143 144bool StringView::ends_with(const StringView& str) const 145{ 146 if (str.is_empty()) 147 return true; 148 if (is_empty()) 149 return false; 150 if (str.length() > length()) 151 return false; 152 return !memcmp(characters_without_null_termination() + length() - str.length(), str.characters_without_null_termination(), str.length()); 153} 154 155bool StringView::matches(const StringView& mask, CaseSensitivity case_sensitivity) const 156{ 157 return StringUtils::matches(*this, mask, case_sensitivity); 158} 159 160StringView StringView::substring_view(size_t start, size_t length) const 161{ 162 ASSERT(start + length <= m_length); 163 return { m_characters + start, length }; 164} 165 166StringView StringView::substring_view_starting_from_substring(const StringView& substring) const 167{ 168 const char* remaining_characters = substring.characters_without_null_termination(); 169 ASSERT(remaining_characters >= m_characters); 170 ASSERT(remaining_characters <= m_characters + m_length); 171 size_t remaining_length = m_length - (remaining_characters - m_characters); 172 return { remaining_characters, remaining_length }; 173} 174 175StringView StringView::substring_view_starting_after_substring(const StringView& substring) const 176{ 177 const char* remaining_characters = substring.characters_without_null_termination() + substring.length(); 178 ASSERT(remaining_characters >= m_characters); 179 ASSERT(remaining_characters <= m_characters + m_length); 180 size_t remaining_length = m_length - (remaining_characters - m_characters); 181 return { remaining_characters, remaining_length }; 182} 183 184int StringView::to_int(bool& ok) const 185{ 186 return StringUtils::convert_to_int(*this, ok); 187} 188 189unsigned StringView::to_uint(bool& ok) const 190{ 191 return StringUtils::convert_to_uint(*this, ok); 192} 193 194unsigned StringView::hash() const 195{ 196 if (is_empty()) 197 return 0; 198 if (m_impl) 199 return m_impl->hash(); 200 return string_hash(characters_without_null_termination(), length()); 201} 202 203bool StringView::operator==(const String& string) const 204{ 205 if (string.is_null()) 206 return !m_characters; 207 if (!m_characters) 208 return false; 209 if (m_length != string.length()) 210 return false; 211 if (m_characters == string.characters()) 212 return true; 213 return !__builtin_memcmp(m_characters, string.characters(), m_length); 214} 215 216}