From d9fb1b8c2ebd1956948e640968b5fcf6ab42a3e2 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 3 Feb 2022 16:52:24 +0000 Subject: LibCore: Make ConfigFile parsing work for non-null-terminated strings This is necessary for converting it to Core::Stream. --- Userland/Libraries/LibCore/ConfigFile.cpp | 43 +++++++++++++++++++------------ 1 file changed, 26 insertions(+), 17 deletions(-) (limited to 'Userland/Libraries/LibCore') diff --git a/Userland/Libraries/LibCore/ConfigFile.cpp b/Userland/Libraries/LibCore/ConfigFile.cpp index e4f3e5cab8..4aad3df7e7 100644 --- a/Userland/Libraries/LibCore/ConfigFile.cpp +++ b/Userland/Libraries/LibCore/ConfigFile.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2021, Jakob-Niklas See + * Copyright (c) 2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -10,7 +11,6 @@ #include #include #include -#include namespace Core { @@ -82,32 +82,41 @@ void ConfigFile::reparse() return; } - auto* cp = line.characters(); + size_t i = 0; - while (*cp && (*cp == ' ' || *cp == '\t' || *cp == '\n')) - ++cp; + while (i < line.length() && (line[i] == ' ' || line[i] == '\t' || line[i] == '\n')) + ++i; - switch (*cp) { - case '\0': // EOL... - case '#': // Comment, skip entire line. - case ';': // -||- + // EOL + if (i >= line.length()) + continue; + + switch (line[i]) { + case '#': // Comment, skip entire line. + case ';': // -||- continue; case '[': { // Start of new group. StringBuilder builder; - ++cp; // Skip the '[' - while (*cp && (*cp != ']')) - builder.append(*(cp++)); + ++i; // Skip the '[' + while (i < line.length() && (line[i] != ']')) { + builder.append(line[i]); + ++i; + } current_group = &m_groups.ensure(builder.to_string()); break; } - default: { // Start of key{ + default: { // Start of key StringBuilder key_builder; StringBuilder value_builder; - while (*cp && (*cp != '=')) - key_builder.append(*(cp++)); - ++cp; // Skip the '=' - while (*cp && (*cp != '\n')) - value_builder.append(*(cp++)); + while (i < line.length() && (line[i] != '=')) { + key_builder.append(line[i]); + ++i; + } + ++i; // Skip the '=' + while (i < line.length() && (line[i] != '\n')) { + value_builder.append(line[i]); + ++i; + } if (!current_group) { // We're not in a group yet, create one with the name ""... current_group = &m_groups.ensure(""); -- cgit v1.2.3