From 5f6dd87163f7c7dded9f936eb0b3886cad3a9573 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Mon, 24 Apr 2023 12:29:31 +0100 Subject: LibChess: Add and use Square::{file,rank}_char() methods This saves us having to build and allocate a String, just to then use one character of it. --- Userland/Libraries/LibChess/Chess.cpp | 17 ++++++++++++++--- Userland/Libraries/LibChess/Chess.h | 4 ++++ 2 files changed, 18 insertions(+), 3 deletions(-) (limited to 'Userland/Libraries/LibChess') diff --git a/Userland/Libraries/LibChess/Chess.cpp b/Userland/Libraries/LibChess/Chess.cpp index 74ee1c0974..cf8d24ee05 100644 --- a/Userland/Libraries/LibChess/Chess.cpp +++ b/Userland/Libraries/LibChess/Chess.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, the SerenityOS developers. + * Copyright (c) 2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -83,6 +84,16 @@ Square::Square(StringView name) } } +char Square::file_char() const +{ + return file + 'a'; +} + +char Square::rank_char() const +{ + return rank + '1'; +} + DeprecatedString Square::to_algebraic() const { StringBuilder builder; @@ -200,16 +211,16 @@ DeprecatedString Move::to_algebraic() const if (is_ambiguous) { if (from.file != ambiguous.file) - builder.append(from.to_algebraic().substring(0, 1)); + builder.append(from.file_char()); else if (from.rank != ambiguous.rank) - builder.append(from.to_algebraic().substring(1, 1)); + builder.append(from.rank_char()); else builder.append(from.to_algebraic()); } if (is_capture) { if (piece.type == Type::Pawn && !is_ambiguous) - builder.append(from.to_algebraic().substring(0, 1)); + builder.append(from.file_char()); builder.append('x'); } diff --git a/Userland/Libraries/LibChess/Chess.h b/Userland/Libraries/LibChess/Chess.h index e2dfb55aa4..554eda0fe3 100644 --- a/Userland/Libraries/LibChess/Chess.h +++ b/Userland/Libraries/LibChess/Chess.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, the SerenityOS developers. + * Copyright (c) 2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -89,6 +90,9 @@ struct Square { bool in_bounds() const { return rank >= 0 && file >= 0 && rank < 8 && file < 8; } bool is_light() const { return (rank % 2) != (file % 2); } + + char file_char() const; + char rank_char() const; DeprecatedString to_algebraic() const; }; -- cgit v1.2.3