LibRegex: Replace fprintf()/printf() with warnln()/outln()/dbgln()

This commit is contained in:
Linus Groh 2021-05-31 15:08:22 +01:00
parent 81b7b2f49e
commit dac0554fa0
Notes: sideshowbarker 2024-07-18 17:06:41 +09:00
5 changed files with 20 additions and 27 deletions

View file

@ -362,7 +362,7 @@ ALWAYS_INLINE ExecutionResult OpCode_SaveRightNamedCaptureGroup::execute(const M
map.set(capture_group_name, { view, input.line, start_position, input.global_offset + start_position }); // take view to original string
}
} else {
fprintf(stderr, "Didn't find corresponding capture group match for name=%s, match_index=%lu\n", capture_group_name.to_string().characters(), input.match_index);
warnln("Didn't find corresponding capture group match for name={}, match_index={}", capture_group_name.to_string(), input.match_index);
}
return ExecutionResult::Continue;
@ -490,7 +490,7 @@ ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(const MatchInput& input, M
return ExecutionResult::Failed_ExecuteLowPrioForks;
} else {
fprintf(stderr, "Undefined comparison: %i\n", (int)compare_type);
warnln("Undefined comparison: {}", (int)compare_type);
VERIFY_NOT_REACHED();
break;
}

View file

@ -27,7 +27,7 @@ public:
auto& bytecode = regex.parser_result.bytecode;
size_t index { 0 };
for (auto& value : bytecode) {
fprintf(m_file, "OpCode i=%3lu [0x%02X]\n", index, (u32)value);
outln(m_file, "OpCode i={:3} [{:#02X}]", index, (u32)value);
++index;
}
}
@ -46,7 +46,7 @@ public:
}
print_opcode("PrintBytecode", *opcode, state);
fprintf(m_file, "%s", m_debug_stripline.characters());
out(m_file, "{}", m_debug_stripline);
if (is<OpCode_Exit>(*opcode))
break;
@ -59,19 +59,18 @@ public:
void print_opcode(const String& system, OpCode& opcode, MatchState& state, size_t recursion = 0, bool newline = true) const
{
fprintf(m_file, "%-15s | %-5lu | %-9lu | %-35s | %-30s | %-20s%s",
out(m_file, "{:15} | {:5} | {:9} | {:35} | {:30} | {:20}",
system.characters(),
state.instruction_position,
recursion,
opcode.to_string().characters(),
opcode.arguments_string().characters(),
String::formatted("ip: {:3}, sp: {:3}", state.instruction_position, state.string_position).characters(),
newline ? "\n" : "");
String::formatted("ip: {:3}, sp: {:3}", state.instruction_position, state.string_position));
if (newline)
outln();
if (newline && is<OpCode_Compare>(opcode)) {
for (auto& line : to<OpCode_Compare>(opcode).variable_arguments_to_string()) {
fprintf(m_file, "%-15s | %-5s | %-9s | %-35s | %-30s | %-20s%s", "", "", "", "", line.characters(), "", "\n");
}
for (auto& line : to<OpCode_Compare>(opcode).variable_arguments_to_string())
outln(m_file, "{:15} | {:5} | {:9} | {:35} | {:30} | {:20}", "", "", "", "", line, "");
}
}
@ -88,15 +87,15 @@ public:
builder.appendff(", next ip: {}", state.instruction_position + opcode.size());
}
fprintf(m_file, " | %-20s\n", builder.to_string().characters());
out(m_file, " | {:20}", builder.to_string());
if (is<OpCode_Compare>(opcode)) {
for (auto& line : to<OpCode_Compare>(opcode).variable_arguments_to_string(input)) {
fprintf(m_file, "%-15s | %-5s | %-9s | %-35s | %-30s | %-20s%s", "", "", "", "", line.characters(), "", "\n");
outln(m_file, "{:15} | {:5} | {:9} | {:35} | {:30} | {:20}", "", "", "", "", line, "");
}
}
fprintf(m_file, "%s", m_debug_stripline.characters());
out(m_file, "{}", m_debug_stripline);
}
void print_header()
@ -110,7 +109,7 @@ public:
auto str = builder.to_string();
VERIFY(!str.is_empty());
fprintf(m_file, "%s\n", str.characters());
outln(m_file, "{}", str);
fflush(m_file);
builder.clear();

View file

@ -7,6 +7,7 @@
#include "RegexLexer.h"
#include <AK/Assertions.h>
#include <AK/Debug.h>
#include <AK/Format.h>
#include <stdio.h>
namespace regex {
@ -130,8 +131,7 @@ Token Lexer::next()
case '\\':
return 2;
default:
if constexpr (REGEX_DEBUG)
fprintf(stderr, "[LEXER] Found invalid escape sequence: \\%c (the parser will have to deal with this!)\n", peek(1));
dbgln_if(REGEX_DEBUG, "[LEXER] Found invalid escape sequence: \\{:c} (the parser will have to deal with this!)", peek(1));
return 0;
}
};

View file

@ -363,16 +363,12 @@ ALWAYS_INLINE Optional<bool> Matcher<Parser>::execute_low_prio_forks(const Match
for (auto& state : states) {
state.instruction_position = state.fork_at_position;
#if REGEX_DEBUG
fprintf(stderr, "Forkstay... ip = %lu, sp = %lu\n", state.instruction_position, state.string_position);
#endif
dbgln_if(REGEX_DEBUG, "Forkstay... ip = {}, sp = {}", state.instruction_position, state.string_position);
auto success = execute(input, state, output, recursion_level);
if (!success.has_value())
return {};
if (success.value()) {
#if REGEX_DEBUG
fprintf(stderr, "Forkstay succeeded... ip = %lu, sp = %lu\n", state.instruction_position, state.string_position);
#endif
dbgln_if(REGEX_DEBUG, "Forkstay succeeded... ip = {}, sp = {}", state.instruction_position, state.string_position);
original_state = state;
return true;
}

View file

@ -148,8 +148,7 @@ Parser::Result Parser::parse(Optional<AllOptions> regex_options)
else
set_error(Error::InvalidPattern);
if constexpr (REGEX_DEBUG)
fprintf(stderr, "[PARSER] Produced bytecode with %lu entries (opcodes + arguments)\n", m_parser_state.bytecode.size());
dbgln_if(REGEX_DEBUG, "[PARSER] Produced bytecode with {} entries (opcodes + arguments)", m_parser_state.bytecode.size());
return {
move(m_parser_state.bytecode),
move(m_parser_state.capture_groups_count),
@ -460,8 +459,7 @@ ALWAYS_INLINE bool PosixExtendedParser::parse_sub_expression(ByteCode& stack, si
if (match(TokenType::EscapeSequence)) {
length = 1;
Token t = consume();
if constexpr (REGEX_DEBUG)
printf("[PARSER] EscapeSequence with substring %s\n", String(t.value()).characters());
dbgln_if(REGEX_DEBUG, "[PARSER] EscapeSequence with substring {}", t.value());
bytecode.insert_bytecode_compare_values({ { CharacterCompareType::Char, (u32)t.value().characters_without_null_termination()[1] } });
should_parse_repetition_symbol = true;