Skip to content

Commit

Permalink
refactor: Improve robustness of string parsing
Browse files Browse the repository at this point in the history
BENCH: 21337504
  • Loading branch information
IbaiBuR committed Oct 15, 2024
1 parent c781d2d commit 84e5a94
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
12 changes: 10 additions & 2 deletions src/board/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "bitboard/attacks.hpp"

#include "../utils/parsing.hpp"
#include "../utils/split.hpp"
#include "../utils/zobrist.hpp"

Expand Down Expand Up @@ -64,8 +65,15 @@ position::position(const std::string& fen) :
en_passant == "-" ? square::none : square_of(en_passant[0] - 'a', en_passant[1] - 1 - '0');
m_key ^= utils::zobrist::get_en_passant_key(m_ep_sq);

m_half_move_clock = std::stoi(tokens[4]);
m_full_move_number = std::stoi(tokens[5]);
if (const auto parsed_half_move_clock = utils::parsing::to_number<u8>(tokens[4]))
m_half_move_clock = parsed_half_move_clock.value();
else
throw std::runtime_error("Failed to parse halfmove clock.\n");

if (const auto parsed_full_move_number = utils::parsing::to_number<u16>(tokens[5]))
m_full_move_number = parsed_full_move_number.value();
else
throw std::runtime_error("Failed to parse fullmove number.\n");

m_hash_history.reserve(constants::max_game_ply - m_full_move_number);

Expand Down
38 changes: 28 additions & 10 deletions src/search/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
#include <cmath>
#include <format>
#include <iostream>
#include <limits>
#include <stdexcept>

#include "tt.hpp"

#include "../eval/eval.hpp"
#include "../moves/movegen.hpp"
#include "../utils/mdarray.hpp"
#include "../utils/parsing.hpp"
#include "../utils/time.hpp"
#include "../utils/score.hpp"

Expand Down Expand Up @@ -62,22 +65,37 @@ void searcher::parse_time_control(const std::vector<std::string>& command, const
u64 base_time{};
u16 increment{};

auto set_base_time_and_limits = [&](const std::optional<u64> parsed_base_time) -> void {
if (parsed_base_time) {
base_time = parsed_base_time.value();
set_limits(std::numeric_limits<u64>::max(), base_time, constants::max_depth);
}
else {
throw std::runtime_error("Failed to parse base time.\n");
}
};

auto set_increment = [&](const std::optional<u16> parsed_increment) -> void {
if (parsed_increment)
increment = parsed_increment.value();
else
throw std::runtime_error("Failed to parse increment.\n");
};

for (auto it = command.begin() + 1; it < command.end(); ++it) {
if (stm == color::white) {
if (*it == "wtime") {
base_time = std::stoull(*(it + 1));
set_limits(UINT64_MAX, base_time, constants::max_depth);
}
if (*it == "wtime")
set_base_time_and_limits(utils::parsing::to_number<u64>(*(it + 1)));

if (*it == "winc")
increment = std::stoi(*(it + 1));
set_increment(utils::parsing::to_number<u16>(*(it + 1)));
}
else {
if (*it == "btime") {
base_time = std::stoull(*(it + 1));
set_limits(UINT64_MAX, base_time, constants::max_depth);
}
if (*it == "btime")
set_base_time_and_limits(utils::parsing::to_number<u64>(*(it + 1)));

if (*it == "binc")
increment = std::stoi(*(it + 1));
set_increment(utils::parsing::to_number<u16>(*(it + 1)));
}
}
m_timer = time_manager(utils::time::get_time_ms(), base_time, increment);
Expand Down
21 changes: 17 additions & 4 deletions src/uci/uci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

#include <format>
#include <iostream>
#include <limits>
#include <numeric>

#include "../eval/eval.hpp"
#include "../moves/movegen.hpp"
#include "../perft/perft.hpp"
#include "../utils/split.hpp"
#include "../search/tt.hpp"
#include "../utils/parsing.hpp"
#include "../utils/time.hpp"

namespace uci {
Expand All @@ -24,19 +26,30 @@ void command_handler::handle_is_ready() { std::cout << "readyok" << std::endl; }
void command_handler::handle_go(const std::vector<std::string>& command,
const board::position& pos) {
if (command[1] == "depth") {
m_searcher.set_limits(UINT64_MAX, UINT64_MAX, std::stoi(command[2]));
if (const auto parsed_depth = utils::parsing::to_number<u32>(command[2]))
m_searcher.set_limits(std::numeric_limits<u64>::max(), std::numeric_limits<u64>::max(),
parsed_depth.value());

m_searcher.set_start_time(utils::time::get_time_ms());
}
else if (command[1] == "perft") {
split_perft(pos, std::stoi(command[2]));
if (const auto parsed_perft_depth = utils::parsing::to_number<int>(command[2]))
split_perft(pos, parsed_perft_depth.value());

return;
}
else if (command[1] == "movetime") {
m_searcher.set_limits(UINT64_MAX, std::stoull(command[2]), constants::max_depth);
if (const auto parsed_move_time = utils::parsing::to_number<u64>(command[2]))
m_searcher.set_limits(std::numeric_limits<u64>::max(), parsed_move_time.value(),
constants::max_depth);

m_searcher.set_start_time(utils::time::get_time_ms());
}
else if (command[1] == "nodes") {
m_searcher.set_limits(std::stoull(command[2]), UINT64_MAX, constants::max_depth);
if (const auto parsed_nodes = utils::parsing::to_number<u64>(command[2]))
m_searcher.set_limits(parsed_nodes.value(), std::numeric_limits<u64>::max(),
constants::max_depth);

m_searcher.set_start_time(utils::time::get_time_ms());
}
else if (command[1] == "wtime" || command[1] == "btime") {
Expand Down

0 comments on commit 84e5a94

Please sign in to comment.