1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 19:38:12 +00:00

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
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 map.set(capture_group_name, { view, input.line, start_position, input.global_offset + start_position }); // take view to original string
} }
} else { } 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; return ExecutionResult::Continue;
@ -490,7 +490,7 @@ ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(const MatchInput& input, M
return ExecutionResult::Failed_ExecuteLowPrioForks; return ExecutionResult::Failed_ExecuteLowPrioForks;
} else { } else {
fprintf(stderr, "Undefined comparison: %i\n", (int)compare_type); warnln("Undefined comparison: {}", (int)compare_type);
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
break; break;
} }

View file

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

View file

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

View file

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

View file

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