diff options
Diffstat (limited to 'Userland/Libraries/LibJS/Bytecode/StringTable.cpp')
-rw-r--r-- | Userland/Libraries/LibJS/Bytecode/StringTable.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Bytecode/StringTable.cpp b/Userland/Libraries/LibJS/Bytecode/StringTable.cpp new file mode 100644 index 0000000000..53a7e3ce76 --- /dev/null +++ b/Userland/Libraries/LibJS/Bytecode/StringTable.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibJS/Bytecode/StringTable.h> + +namespace JS::Bytecode { + +StringTableIndex StringTable::insert(StringView string) +{ + for (size_t i = 0; i < m_strings.size(); i++) { + if (m_strings[i] == string) + return i; + } + m_strings.append(string); + return m_strings.size() - 1; +} + +String const& StringTable::get(StringTableIndex index) const +{ + return m_strings[index.value()]; +} + +void StringTable::dump() const +{ + outln("String Table:"); + for (size_t i = 0; i < m_strings.size(); i++) + outln("{}: {}", i, m_strings[i]); +} + +} |