mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 23:50:19 +00:00
6613a4cb8c
The symbol name insertion scheme is different from objdump -d's. Compare the output on Build/Userland/id: * disasm: ... _start (08048305-0804836b): 08048305 push ebp ... 08048366 call 0x0000df56 0804836b o16 nop 0804836d o16 nop 0804836f nop (deregister_tm_clones (08048370-08048370)) 08048370 mov eax, 0x080643e0 ... _ZN2AK8Utf8ViewC1ERKNS_6StringE (0805d9b2-0805d9b7): _ZN2AK8Utf8ViewC2ERKNS_6StringE (0805d9b2-0805d9b7): 0805d9b2 jmp 0x00014ff2 0805d9b7 nop * objdump -d: 08048305 <_start>: 8048305: 55 push %ebp ... 8048366: e8 9b dc 00 00 call 8056006 <exit> 804836b: 66 90 xchg %ax,%ax 804836d: 66 90 xchg %ax,%ax 804836f: 90 nop 08048370 <deregister_tm_clones>: 8048370: b8 e0 43 06 08 mov $0x80643e0,%eax ... 0805d9b2 <_ZN2AK8Utf8ViewC1ERKNS_6StringE>: 805d9b2: e9 eb f6 ff ff jmp 805d0a2 <_ZN2AK10StringViewC1ERKNS_6StringE> 805d9b7: 90 nop Differences: 1. disasm can show multiple symbols that cover the same instructions. I've only seen this happen for C1/C2 (and D1/D2) ctor/dtor pairs, but it could conceivably happen with ICF as well. 2. disasm separates instructions that do not belong to a symbol with a newline, so that nop padding isn't shown as part of a function when it technically isn't. 3. disasm shows symbols that are skipped (due to having size 0) in parenthesis, separated from preceding and following instructions.
184 lines
6.8 KiB
C++
184 lines
6.8 KiB
C++
/*
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
* 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT HOLDER 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.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Assertions.h>
|
|
#include <AK/Checked.h>
|
|
#include <AK/Forward.h>
|
|
#include <AK/Span.h>
|
|
#include <AK/StdLibExtras.h>
|
|
#include <AK/StringUtils.h>
|
|
|
|
namespace AK {
|
|
|
|
class StringView {
|
|
public:
|
|
using ConstIterator = const char*;
|
|
|
|
ALWAYS_INLINE constexpr StringView() { }
|
|
ALWAYS_INLINE constexpr StringView(const char* characters, size_t length)
|
|
: m_characters(characters)
|
|
, m_length(length)
|
|
{
|
|
ASSERT(!Checked<uintptr_t>::addition_would_overflow((uintptr_t)characters, length));
|
|
}
|
|
ALWAYS_INLINE StringView(const unsigned char* characters, size_t length)
|
|
: m_characters((const char*)characters)
|
|
, m_length(length)
|
|
{
|
|
ASSERT(!Checked<uintptr_t>::addition_would_overflow((uintptr_t)characters, length));
|
|
}
|
|
ALWAYS_INLINE constexpr StringView(const char* cstring)
|
|
: m_characters(cstring)
|
|
, m_length(cstring ? __builtin_strlen(cstring) : 0)
|
|
{
|
|
}
|
|
|
|
StringView(const ByteBuffer&);
|
|
StringView(const String&);
|
|
StringView(const FlyString&);
|
|
|
|
bool is_null() const { return !m_characters; }
|
|
bool is_empty() const { return m_length == 0; }
|
|
|
|
const char* characters_without_null_termination() const { return m_characters; }
|
|
size_t length() const { return m_length; }
|
|
|
|
ReadonlyBytes bytes() const { return { m_characters, m_length }; }
|
|
|
|
const char& operator[](size_t index) const { return m_characters[index]; }
|
|
|
|
ConstIterator begin() const { return characters_without_null_termination(); }
|
|
ConstIterator end() const { return begin() + length(); }
|
|
|
|
unsigned hash() const;
|
|
|
|
bool starts_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
|
bool ends_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
|
bool starts_with(char) const;
|
|
bool ends_with(char) const;
|
|
bool matches(const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
|
|
bool contains(char) const;
|
|
bool contains(const StringView&) const;
|
|
bool equals_ignoring_case(const StringView& other) const;
|
|
|
|
Optional<size_t> find_first_of(char) const;
|
|
Optional<size_t> find_first_of(const StringView&) const;
|
|
|
|
Optional<size_t> find_last_of(char) const;
|
|
Optional<size_t> find_last_of(const StringView&) const;
|
|
|
|
StringView substring_view(size_t start, size_t length) const;
|
|
Vector<StringView> split_view(char, bool keep_empty = false) const;
|
|
Vector<StringView> split_view(const StringView&, bool keep_empty = false) const;
|
|
|
|
// Create a Vector of StringViews split by line endings. As of CommonMark
|
|
// 0.29, the spec defines a line ending as "a newline (U+000A), a carriage
|
|
// return (U+000D) not followed by a newline, or a carriage return and a
|
|
// following newline.".
|
|
Vector<StringView> lines(bool consider_cr = true) const;
|
|
|
|
Optional<int> to_int() const;
|
|
Optional<unsigned> to_uint() const;
|
|
|
|
// Create a new substring view of this string view, starting either at the beginning of
|
|
// the given substring view, or after its end, and continuing until the end of this string
|
|
// view (that is, for the remaining part of its length). For example,
|
|
//
|
|
// StringView str { "foobar" };
|
|
// StringView substr = str.substring_view(1, 2); // "oo"
|
|
// StringView substr_from = str.substring_view_starting_from_substring(subst); // "oobar"
|
|
// StringView substr_after = str.substring_view_starting_after_substring(subst); // "bar"
|
|
//
|
|
// Note that this only works if the string view passed as an argument is indeed a substring
|
|
// view of this string view, such as one created by substring_view() and split_view(). It
|
|
// does not work for arbitrary strings; for example declaring substr in the example above as
|
|
//
|
|
// StringView substr { "oo" };
|
|
//
|
|
// would not work.
|
|
StringView substring_view_starting_from_substring(const StringView& substring) const;
|
|
StringView substring_view_starting_after_substring(const StringView& substring) const;
|
|
|
|
bool operator==(const char* cstring) const
|
|
{
|
|
if (is_null())
|
|
return !cstring;
|
|
if (!cstring)
|
|
return false;
|
|
size_t other_length = __builtin_strlen(cstring);
|
|
if (m_length != other_length)
|
|
return false;
|
|
return !__builtin_memcmp(m_characters, cstring, m_length);
|
|
}
|
|
bool operator!=(const char* cstring) const
|
|
{
|
|
return !(*this == cstring);
|
|
}
|
|
|
|
bool operator==(const String&) const;
|
|
|
|
bool operator==(const StringView& other) const
|
|
{
|
|
if (is_null())
|
|
return other.is_null();
|
|
if (other.is_null())
|
|
return false;
|
|
if (length() != other.length())
|
|
return false;
|
|
return !__builtin_memcmp(m_characters, other.m_characters, m_length);
|
|
}
|
|
|
|
bool operator!=(const StringView& other) const
|
|
{
|
|
return !(*this == other);
|
|
}
|
|
|
|
bool operator<(const StringView& other) const
|
|
{
|
|
if (int c = __builtin_memcmp(m_characters, other.m_characters, min(m_length, other.m_length)))
|
|
return c < 0;
|
|
return m_length < other.m_length;
|
|
}
|
|
|
|
const StringImpl* impl() const { return m_impl; }
|
|
|
|
String to_string() const;
|
|
|
|
const char* begin() { return m_characters; }
|
|
const char* end() { return m_characters + m_length; }
|
|
|
|
private:
|
|
friend class String;
|
|
const StringImpl* m_impl { nullptr };
|
|
const char* m_characters { nullptr };
|
|
size_t m_length { 0 };
|
|
};
|
|
|
|
}
|
|
|
|
using AK::StringView;
|