mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-20 03:25:13 +00:00
StringView: Rename characters() to characters_without_null_termination().
This should make you think twice before trying to use the const char* from a StringView as if it's a null-terminated string.
This commit is contained in:
parent
567551bc12
commit
0e75aba7c3
Notes:
sideshowbarker
2024-07-19 13:22:04 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/0e75aba7c39
21 changed files with 57 additions and 46 deletions
|
@ -41,7 +41,7 @@ public:
|
|||
if (view.m_impl)
|
||||
m_impl = *view.m_impl;
|
||||
else
|
||||
m_impl = StringImpl::create(view.characters(), view.length());
|
||||
m_impl = StringImpl::create(view.characters_without_null_termination(), view.length());
|
||||
}
|
||||
|
||||
String(const String& other)
|
||||
|
|
|
@ -12,7 +12,7 @@ const LogStream& operator<<(const LogStream& stream, const String& value)
|
|||
|
||||
const LogStream& operator<<(const LogStream& stream, const StringView& value)
|
||||
{
|
||||
stream.write(value.characters(), value.length());
|
||||
stream.write(value.characters_without_null_termination(), value.length());
|
||||
return stream;
|
||||
}
|
||||
|
||||
|
|
|
@ -198,7 +198,7 @@ bool String::starts_with(const StringView& str) const
|
|||
return false;
|
||||
if (str.length() > length())
|
||||
return false;
|
||||
return !memcmp(characters(), str.characters(), str.length());
|
||||
return !memcmp(characters(), str.characters_without_null_termination(), str.length());
|
||||
}
|
||||
|
||||
bool String::ends_with(const StringView& str) const
|
||||
|
@ -209,7 +209,7 @@ bool String::ends_with(const StringView& str) const
|
|||
return false;
|
||||
if (str.length() > length())
|
||||
return false;
|
||||
return !memcmp(characters() + (length() - str.length()), str.characters(), str.length());
|
||||
return !memcmp(characters() + (length() - str.length()), str.characters_without_null_termination(), str.length());
|
||||
}
|
||||
|
||||
String String::repeated(char ch, int count)
|
||||
|
@ -239,7 +239,7 @@ bool String::match_helper(const StringView& mask) const
|
|||
return false;
|
||||
|
||||
const char* string_ptr = characters();
|
||||
const char* mask_ptr = mask.characters();
|
||||
const char* mask_ptr = mask.characters_without_null_termination();
|
||||
const char* mask_end = mask_ptr + mask.length();
|
||||
|
||||
// Match string against mask directly unless we hit a *
|
||||
|
|
|
@ -21,7 +21,7 @@ void StringBuilder::append(const StringView& str)
|
|||
if (str.is_empty())
|
||||
return;
|
||||
will_append(str.length());
|
||||
memcpy(m_buffer.pointer() + m_length, str.characters(), str.length());
|
||||
memcpy(m_buffer.pointer() + m_length, str.characters_without_null_termination(), str.length());
|
||||
m_length += str.length();
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ Vector<StringView> StringView::split_view(const char separator) const
|
|||
Vector<StringView> v;
|
||||
ssize_t substart = 0;
|
||||
for (ssize_t i = 0; i < length(); ++i) {
|
||||
char ch = characters()[i];
|
||||
char ch = characters_without_null_termination()[i];
|
||||
if (ch == separator) {
|
||||
ssize_t sublen = i - substart;
|
||||
if (sublen != 0)
|
||||
|
@ -35,7 +35,7 @@ Vector<StringView> StringView::split_view(const char separator) const
|
|||
ssize_t taillen = length() - substart;
|
||||
if (taillen != 0)
|
||||
v.append(substring_view(substart, taillen));
|
||||
if (characters()[length() - 1] == separator)
|
||||
if (characters_without_null_termination()[length() - 1] == separator)
|
||||
v.append(String::empty());
|
||||
return v;
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ StringView StringView::substring_view(int start, int length) const
|
|||
|
||||
StringView StringView::substring_view_starting_from_substring(const StringView& substring) const
|
||||
{
|
||||
const char* remaining_characters = substring.characters();
|
||||
const char* remaining_characters = substring.characters_without_null_termination();
|
||||
ASSERT(remaining_characters >= m_characters);
|
||||
ASSERT(remaining_characters <= m_characters + m_length);
|
||||
int remaining_length = m_length - (remaining_characters - m_characters);
|
||||
|
@ -59,7 +59,7 @@ StringView StringView::substring_view_starting_from_substring(const StringView&
|
|||
|
||||
StringView StringView::substring_view_starting_after_substring(const StringView& substring) const
|
||||
{
|
||||
const char* remaining_characters = substring.characters() + substring.length();
|
||||
const char* remaining_characters = substring.characters_without_null_termination() + substring.length();
|
||||
ASSERT(remaining_characters >= m_characters);
|
||||
ASSERT(remaining_characters <= m_characters + m_length);
|
||||
int remaining_length = m_length - (remaining_characters - m_characters);
|
||||
|
@ -70,12 +70,12 @@ unsigned StringView::to_uint(bool& ok) const
|
|||
{
|
||||
unsigned value = 0;
|
||||
for (ssize_t i = 0; i < length(); ++i) {
|
||||
if (characters()[i] < '0' || characters()[i] > '9') {
|
||||
if (characters_without_null_termination()[i] < '0' || characters_without_null_termination()[i] > '9') {
|
||||
ok = false;
|
||||
return 0;
|
||||
}
|
||||
value = value * 10;
|
||||
value += characters()[i] - '0';
|
||||
value += characters_without_null_termination()[i] - '0';
|
||||
}
|
||||
ok = true;
|
||||
return value;
|
||||
|
|
|
@ -35,7 +35,7 @@ public:
|
|||
|
||||
bool is_null() const { return !m_characters; }
|
||||
bool is_empty() const { return m_length == 0; }
|
||||
const char* characters() const { return m_characters; }
|
||||
const char* characters_without_null_termination() const { return m_characters; }
|
||||
int length() const { return m_length; }
|
||||
char operator[](int index) const { return m_characters[index]; }
|
||||
|
||||
|
|
|
@ -53,7 +53,7 @@ ToolboxWidget::ToolboxWidget(GWidget* parent)
|
|||
button->set_checkable(true);
|
||||
button->set_exclusive(true);
|
||||
|
||||
button->set_icon(load_png(String::format("/res/icons/paintbrush/%s.png", icon_name.characters())));
|
||||
button->set_icon(load_png(String::format("/res/icons/paintbrush/%s.png", String(icon_name).characters())));
|
||||
|
||||
button->on_checked = [button](auto checked) {
|
||||
if (checked)
|
||||
|
|
|
@ -761,7 +761,7 @@ KResult Ext2FSInode::add_child(InodeIdentifier child_id, const StringView& name,
|
|||
ASSERT(is_directory());
|
||||
|
||||
//#ifdef EXT2_DEBUG
|
||||
dbgprintf("Ext2FS: Adding inode %u with name '%s' and mode %o to directory %u\n", child_id.index(), name.characters(), mode, index());
|
||||
dbg() << "Ext2FSInode::add_child(): Adding inode " << child_id.index() << " with name '" << name << " and mode " << mode << " to directory " << index();
|
||||
//#endif
|
||||
|
||||
Vector<FS::DirectoryEntry> entries;
|
||||
|
@ -775,7 +775,7 @@ KResult Ext2FSInode::add_child(InodeIdentifier child_id, const StringView& name,
|
|||
return true;
|
||||
});
|
||||
if (name_already_exists) {
|
||||
kprintf("Ext2FS: Name '%s' already exists in directory inode %u\n", name.characters(), index());
|
||||
dbg() << "Ext2FSInode::add_child(): Name '" << name << "' already exists in inode " << index();
|
||||
return KResult(-EEXIST);
|
||||
}
|
||||
|
||||
|
@ -783,7 +783,7 @@ KResult Ext2FSInode::add_child(InodeIdentifier child_id, const StringView& name,
|
|||
if (child_inode)
|
||||
child_inode->increment_link_count();
|
||||
|
||||
entries.append({ name.characters(), name.length(), child_id, to_ext2_file_type(mode) });
|
||||
entries.append({ name.characters_without_null_termination(), name.length(), child_id, to_ext2_file_type(mode) });
|
||||
bool success = write_directory(entries);
|
||||
if (success)
|
||||
m_lookup_cache.set(name, child_id.index());
|
||||
|
@ -794,7 +794,7 @@ KResult Ext2FSInode::remove_child(const StringView& name)
|
|||
{
|
||||
LOCKER(m_lock);
|
||||
#ifdef EXT2_DEBUG
|
||||
dbgprintf("Ext2FSInode::remove_child(%s) in inode %u\n", name.characters(), index());
|
||||
dbg() << "Ext2FSInode::remove_child(" << name << ") in inode " << index();
|
||||
#endif
|
||||
ASSERT(is_directory());
|
||||
|
||||
|
@ -807,7 +807,7 @@ KResult Ext2FSInode::remove_child(const StringView& name)
|
|||
InodeIdentifier child_id { fsid(), child_inode_index };
|
||||
|
||||
//#ifdef EXT2_DEBUG
|
||||
dbgprintf("Ext2FS: Removing '%s' in directory %u\n", name.characters(), index());
|
||||
dbg() << "Ext2FSInode::remove_child(): Removing '" << name << "' in directory " << index();
|
||||
//#endif
|
||||
|
||||
Vector<FS::DirectoryEntry> entries;
|
||||
|
|
|
@ -42,3 +42,10 @@ private:
|
|||
u32 m_fsid { 0 };
|
||||
u32 m_index { 0 };
|
||||
};
|
||||
|
||||
inline const LogStream& operator<<(const LogStream& stream, const InodeIdentifier& value)
|
||||
{
|
||||
stream << value.fsid() << ':' << value.index();
|
||||
return stream;
|
||||
}
|
||||
|
||||
|
|
|
@ -40,11 +40,11 @@ bool VFS::mount(NonnullRefPtr<FS>&& file_system, StringView path)
|
|||
{
|
||||
auto result = resolve_path(path, root_custody());
|
||||
if (result.is_error()) {
|
||||
kprintf("VFS: mount can't resolve mount point '%s'\n", path.characters());
|
||||
dbg() << "VFS: mount can't resolve mount point '" << path << "'";
|
||||
return false;
|
||||
}
|
||||
auto& inode = result.value()->inode();
|
||||
kprintf("VFS: mounting %s{%p} at %s (inode: %u)\n", file_system->class_name(), file_system.ptr(), path.characters(), inode.index());
|
||||
dbg() << "VFS: Mounting " << file_system->class_name() << " at " << path << " (inode: " << inode.identifier() << ")";
|
||||
// FIXME: check that this is not already a mount point
|
||||
auto mount = make<Mount>(*result.value(), move(file_system));
|
||||
m_mounts.append(move(mount));
|
||||
|
@ -221,7 +221,7 @@ KResult VFS::mknod(StringView path, mode_t mode, dev_t dev, Custody& base)
|
|||
return KResult(-EACCES);
|
||||
|
||||
FileSystemPath p(path);
|
||||
dbgprintf("VFS::mknod: '%s' mode=%o dev=%u in %u:%u\n", p.basename().characters(), mode, dev, parent_inode.fsid(), parent_inode.index());
|
||||
dbg() << "VFS::mknod: '" << p.basename() << "' mode=" << mode << " dev=" << dev << " in " << parent_inode.identifier();
|
||||
int error;
|
||||
auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), mode, 0, dev, error);
|
||||
if (!new_file)
|
||||
|
@ -243,7 +243,7 @@ KResultOr<NonnullRefPtr<FileDescription>> VFS::create(StringView path, int optio
|
|||
if (!parent_inode.metadata().may_write(current->process()))
|
||||
return KResult(-EACCES);
|
||||
FileSystemPath p(path);
|
||||
dbgprintf("VFS::create_file: '%s' in %u:%u\n", p.basename().characters(), parent_inode.fsid(), parent_inode.index());
|
||||
dbg() << "VFS::create: '" << p.basename() << "' in " << parent_inode.identifier();
|
||||
int error;
|
||||
auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), mode, 0, 0, error);
|
||||
if (!new_file)
|
||||
|
@ -269,7 +269,7 @@ KResult VFS::mkdir(StringView path, mode_t mode, Custody& base)
|
|||
return KResult(-EACCES);
|
||||
|
||||
FileSystemPath p(path);
|
||||
dbgprintf("VFS::mkdir: '%s' in %u:%u\n", p.basename().characters(), parent_inode.fsid(), parent_inode.index());
|
||||
dbg() << "VFS::mkdir: '" << p.basename() << "' in " << parent_inode.identifier();
|
||||
int error;
|
||||
auto new_dir = parent_inode.fs().create_directory(parent_inode.identifier(), p.basename(), mode, error);
|
||||
if (new_dir)
|
||||
|
@ -423,7 +423,7 @@ KResult VFS::chown(Inode& inode, uid_t a_uid, gid_t a_gid)
|
|||
new_gid = a_gid;
|
||||
}
|
||||
|
||||
dbgprintf("VFS::chown(): inode %u:%u <- uid:%d, gid:%d\n", inode.fsid(), inode.index(), new_uid, new_gid);
|
||||
dbg() << "VFS::chown(): inode " << inode.identifier() << " <- uid:" << new_uid << " gid:" << new_gid;
|
||||
return inode.chown(new_uid, new_gid);
|
||||
}
|
||||
|
||||
|
@ -511,12 +511,12 @@ KResult VFS::symlink(StringView target, StringView linkpath, Custody& base)
|
|||
return KResult(-EACCES);
|
||||
|
||||
FileSystemPath p(linkpath);
|
||||
dbgprintf("VFS::symlink: '%s' (-> '%s') in %u:%u\n", p.basename().characters(), target.characters(), parent_inode.fsid(), parent_inode.index());
|
||||
dbg() << "VFS::symlink: '" << p.basename() << "' (-> '" << target << "') in " << parent_inode.identifier();
|
||||
int error;
|
||||
auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), 0120644, 0, 0, error);
|
||||
if (!new_file)
|
||||
return KResult(error);
|
||||
ssize_t nwritten = new_file->write_bytes(0, target.length(), (const u8*)target.characters(), nullptr);
|
||||
ssize_t nwritten = new_file->write_bytes(0, target.length(), (const u8*)target.characters_without_null_termination(), nullptr);
|
||||
if (nwritten < 0)
|
||||
return KResult(nwritten);
|
||||
return KSuccess;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <AK/HashTable.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <Kernel/Lock.h>
|
||||
#include <Kernel/Net/EtherType.h>
|
||||
#include <Kernel/Net/EthernetFrameHeader.h>
|
||||
|
@ -100,7 +101,10 @@ void NetworkAdapter::set_ipv4_address(const IPv4Address& address)
|
|||
void NetworkAdapter::set_interface_name(const StringView& basename)
|
||||
{
|
||||
// FIXME: Find a unique name for this interface, starting with $basename.
|
||||
m_name = String::format("%s0", basename.characters());
|
||||
StringBuilder builder;
|
||||
builder.append(basename);
|
||||
builder.append('0');
|
||||
m_name = builder.to_string();
|
||||
}
|
||||
|
||||
bool PacketQueueAlarm::is_ringing() const
|
||||
|
|
|
@ -32,7 +32,7 @@ public:
|
|||
ByteBuffer read_all();
|
||||
|
||||
bool write(const u8*, int size);
|
||||
bool write(const AK::StringView& v) { return write((const u8*)v.characters(), v.length()); }
|
||||
bool write(const StringView& v) { return write((const u8*)v.characters_without_null_termination(), v.length()); }
|
||||
|
||||
// FIXME: I would like this to be const but currently it needs to call populate_read_buffer().
|
||||
bool can_read_line();
|
||||
|
|
|
@ -44,7 +44,7 @@ void GClipboard::set_data(const StringView& data)
|
|||
return;
|
||||
}
|
||||
if (!data.is_empty())
|
||||
memcpy(shared_buffer->data(), data.characters(), data.length() + 1);
|
||||
memcpy(shared_buffer->data(), data.characters_without_null_termination(), data.length() + 1);
|
||||
else
|
||||
((u8*)shared_buffer->data())[0] = '\0';
|
||||
shared_buffer->seal();
|
||||
|
|
|
@ -29,7 +29,7 @@ bool GDesktop::set_wallpaper(const StringView& path)
|
|||
WSAPI_ClientMessage message;
|
||||
message.type = WSAPI_ClientMessage::Type::SetWallpaper;
|
||||
ASSERT(path.length() < (int)sizeof(message.text));
|
||||
strncpy(message.text, path.characters(), path.length());
|
||||
strncpy(message.text, path.characters_without_null_termination(), path.length());
|
||||
message.text_length = path.length();
|
||||
auto response = GEventLoop::current().sync_request(message, WSAPI_ServerMessage::Type::DidSetWallpaper);
|
||||
return response.value;
|
||||
|
|
|
@ -64,7 +64,7 @@ void GIconImpl::set_bitmap_for_size(int size, RefPtr<GraphicsBitmap>&& bitmap)
|
|||
|
||||
GIcon GIcon::default_icon(const StringView& name)
|
||||
{
|
||||
auto bitmap16 = GraphicsBitmap::load_from_file(String::format("/res/icons/16x16/%s.png", name.characters()));
|
||||
auto bitmap32 = GraphicsBitmap::load_from_file(String::format("/res/icons/32x32/%s.png", name.characters()));
|
||||
auto bitmap16 = GraphicsBitmap::load_from_file(String::format("/res/icons/16x16/%s.png", String(name).characters()));
|
||||
auto bitmap32 = GraphicsBitmap::load_from_file(String::format("/res/icons/32x32/%s.png", String(name).characters()));
|
||||
return GIcon(move(bitmap16), move(bitmap32));
|
||||
}
|
||||
|
|
|
@ -68,7 +68,7 @@ void GTextEditor::create_actions()
|
|||
|
||||
void GTextEditor::set_text(const StringView& text)
|
||||
{
|
||||
if (is_single_line() && text.length() == m_lines[0]->length() && !memcmp(text.characters(), m_lines[0]->characters(), text.length()))
|
||||
if (is_single_line() && text.length() == m_lines[0]->length() && !memcmp(text.characters_without_null_termination(), m_lines[0]->characters(), text.length()))
|
||||
return;
|
||||
|
||||
m_selection.clear();
|
||||
|
@ -783,14 +783,14 @@ void GTextEditor::Line::clear()
|
|||
|
||||
void GTextEditor::Line::set_text(const StringView& text)
|
||||
{
|
||||
if (text.length() == length() && !memcmp(text.characters(), characters(), length()))
|
||||
if (text.length() == length() && !memcmp(text.characters_without_null_termination(), characters(), length()))
|
||||
return;
|
||||
if (text.is_empty()) {
|
||||
clear();
|
||||
return;
|
||||
}
|
||||
m_text.resize(text.length() + 1);
|
||||
memcpy(m_text.data(), text.characters(), text.length() + 1);
|
||||
memcpy(m_text.data(), text.characters_without_null_termination(), text.length() + 1);
|
||||
}
|
||||
|
||||
int GTextEditor::Line::width(const Font& font) const
|
||||
|
@ -844,7 +844,7 @@ void GTextEditor::Line::truncate(int length)
|
|||
|
||||
bool GTextEditor::write_to_file(const StringView& path)
|
||||
{
|
||||
int fd = open(path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
||||
int fd = open(String(path).characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
||||
if (fd < 0) {
|
||||
perror("open");
|
||||
return false;
|
||||
|
|
|
@ -627,7 +627,7 @@ void GWindow::set_icon_path(const StringView& path)
|
|||
message.type = WSAPI_ClientMessage::Type::SetWindowIcon;
|
||||
message.window_id = m_window_id;
|
||||
ASSERT(path.length() < (int)sizeof(message.text));
|
||||
strcpy(message.text, path.characters());
|
||||
strcpy(message.text, String(path).characters());
|
||||
message.text_length = path.length();
|
||||
GEventLoop::post_message_to_server(message);
|
||||
}
|
||||
|
|
|
@ -127,7 +127,7 @@ RefPtr<Font> Font::load_from_file(const StringView& path)
|
|||
|
||||
bool Font::write_to_file(const StringView& path)
|
||||
{
|
||||
int fd = creat(path.characters(), 0644);
|
||||
int fd = creat(String(path).characters(), 0644);
|
||||
if (fd < 0) {
|
||||
perror("open");
|
||||
return false;
|
||||
|
@ -169,7 +169,7 @@ int Font::width(const StringView& string) const
|
|||
|
||||
int width = 0;
|
||||
for (int i = 0; i < string.length(); ++i)
|
||||
width += glyph_width(string.characters()[i]) + 1;
|
||||
width += glyph_width(string.characters_without_null_termination()[i]) + 1;
|
||||
|
||||
return width - 1;
|
||||
}
|
||||
|
|
|
@ -89,7 +89,7 @@ GraphicsBitmap::~GraphicsBitmap()
|
|||
void GraphicsBitmap::set_mmap_name(const StringView& name)
|
||||
{
|
||||
ASSERT(m_needs_munmap);
|
||||
::set_mmap_name(m_data, size_in_bytes(), name.characters());
|
||||
::set_mmap_name(m_data, size_in_bytes(), String(name).characters());
|
||||
}
|
||||
|
||||
void GraphicsBitmap::fill(Color color)
|
||||
|
|
|
@ -571,7 +571,7 @@ void Painter::draw_text(const Rect& rect, const StringView& text, const Font& fo
|
|||
int new_width = font.width("...");
|
||||
if (new_width < text_width) {
|
||||
for (int i = 0; i < final_text.length(); ++i) {
|
||||
int glyph_width = font.glyph_width(final_text.characters()[i]);
|
||||
int glyph_width = font.glyph_width(final_text.characters_without_null_termination()[i]);
|
||||
// NOTE: Glyph spacing should not be added after the last glyph on the line,
|
||||
// but since we are here because the last glyph does not actually fit on the line,
|
||||
// we don't have to worry about spacing.
|
||||
|
@ -582,7 +582,7 @@ void Painter::draw_text(const Rect& rect, const StringView& text, const Font& fo
|
|||
new_width += glyph_width + glyph_spacing;
|
||||
}
|
||||
StringBuilder builder;
|
||||
builder.append(StringView(final_text.characters(), new_length));
|
||||
builder.append(StringView(final_text.characters_without_null_termination(), new_length));
|
||||
builder.append("...");
|
||||
elided_text = builder.to_string();
|
||||
final_text = elided_text;
|
||||
|
@ -609,7 +609,7 @@ void Painter::draw_text(const Rect& rect, const StringView& text, const Font& fo
|
|||
|
||||
int space_width = font.glyph_width(' ') + font.glyph_spacing();
|
||||
for (ssize_t i = 0; i < final_text.length(); ++i) {
|
||||
char ch = final_text.characters()[i];
|
||||
char ch = final_text.characters_without_null_termination()[i];
|
||||
if (ch == ' ') {
|
||||
point.move_by(space_width, 0);
|
||||
continue;
|
||||
|
|
|
@ -224,7 +224,7 @@ struct CommandTimer {
|
|||
static bool is_glob(const StringView& s)
|
||||
{
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.characters()[i];
|
||||
char c = s.characters_without_null_termination()[i];
|
||||
if (c == '*' || c == '?')
|
||||
return true;
|
||||
}
|
||||
|
@ -237,7 +237,7 @@ static Vector<StringView> split_path(const StringView &path)
|
|||
|
||||
ssize_t substart = 0;
|
||||
for (ssize_t i = 0; i < path.length(); i++) {
|
||||
char ch = path.characters()[i];
|
||||
char ch = path.characters_without_null_termination()[i];
|
||||
if (ch != '/')
|
||||
continue;
|
||||
ssize_t sublen = i - substart;
|
||||
|
|
Loading…
Add table
Reference in a new issue