mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:37:37 +00:00
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.
This commit is contained in:
parent
c73c697f94
commit
5f6dd87163
4 changed files with 35 additions and 13 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2020, the SerenityOS developers.
|
||||
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
||||
*
|
||||
* 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');
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue