Browse Source

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.
Sam Atkins 2 năm trước cách đây
mục cha
commit
5f6dd87163

+ 8 - 5
Userland/Applications/GamesSettings/ChessSettingsWidget.cpp

@@ -180,15 +180,18 @@ private:
                 painter.fill_rect(square_rect, square.is_light() ? m_light_square_color : m_dark_square_color);
                 painter.fill_rect(square_rect, square.is_light() ? m_light_square_color : m_dark_square_color);
 
 
                 if (m_show_coordinates) {
                 if (m_show_coordinates) {
-                    auto coord = square.to_algebraic();
                     auto text_color = square.is_light() ? m_dark_square_color : m_light_square_color;
                     auto text_color = square.is_light() ? m_dark_square_color : m_light_square_color;
                     auto shrunken_rect = square_rect.shrunken(4, 4);
                     auto shrunken_rect = square_rect.shrunken(4, 4);
 
 
-                    if (square.rank == 0)
-                        painter.draw_text(shrunken_rect, coord.substring_view(0, 1), coordinate_font, Gfx::TextAlignment::BottomRight, text_color);
+                    if (square.rank == 0) {
+                        auto file_char = square.file_char();
+                        painter.draw_text(shrunken_rect, { &file_char, 1 }, coordinate_font, Gfx::TextAlignment::BottomRight, text_color);
+                    }
 
 
-                    if (square.file == 0)
-                        painter.draw_text(shrunken_rect, coord.substring_view(1, 1), coordinate_font, Gfx::TextAlignment::TopLeft, text_color);
+                    if (square.file == 0) {
+                        auto rank_char = square.rank_char();
+                        painter.draw_text(shrunken_rect, { &rank_char, 1 }, coordinate_font, Gfx::TextAlignment::TopLeft, text_color);
+                    }
                 }
                 }
             }
             }
         }
         }

+ 9 - 5
Userland/Games/Chess/ChessWidget.cpp

@@ -66,16 +66,20 @@ void ChessWidget::paint_event(GUI::PaintEvent& event)
         }
         }
 
 
         if (m_coordinates) {
         if (m_coordinates) {
-            auto coord = sq.to_algebraic();
             auto text_color = (sq.is_light()) ? board_theme().dark_square_color : board_theme().light_square_color;
             auto text_color = (sq.is_light()) ? board_theme().dark_square_color : board_theme().light_square_color;
 
 
             auto shrunken_rect = tile_rect;
             auto shrunken_rect = tile_rect;
             shrunken_rect.shrink(4, 4);
             shrunken_rect.shrink(4, 4);
-            if (sq.rank == coord_rank_file)
-                painter.draw_text(shrunken_rect, coord.substring_view(0, 1), coordinate_font, Gfx::TextAlignment::BottomRight, text_color);
 
 
-            if (sq.file == coord_rank_file)
-                painter.draw_text(shrunken_rect, coord.substring_view(1, 1), coordinate_font, Gfx::TextAlignment::TopLeft, text_color);
+            if (sq.rank == coord_rank_file) {
+                auto file_char = sq.file_char();
+                painter.draw_text(shrunken_rect, { &file_char, 1 }, coordinate_font, Gfx::TextAlignment::BottomRight, text_color);
+            }
+
+            if (sq.file == coord_rank_file) {
+                auto rank_char = sq.rank_char();
+                painter.draw_text(shrunken_rect, { &rank_char, 1 }, coordinate_font, Gfx::TextAlignment::TopLeft, text_color);
+            }
         }
         }
 
 
         for (auto& m : m_board_markings) {
         for (auto& m : m_board_markings) {

+ 14 - 3
Userland/Libraries/LibChess/Chess.cpp

@@ -1,5 +1,6 @@
 /*
 /*
  * Copyright (c) 2020, the SerenityOS developers.
  * Copyright (c) 2020, the SerenityOS developers.
+ * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  *
  *
  * SPDX-License-Identifier: BSD-2-Clause
  * 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
 DeprecatedString Square::to_algebraic() const
 {
 {
     StringBuilder builder;
     StringBuilder builder;
@@ -200,16 +211,16 @@ DeprecatedString Move::to_algebraic() const
 
 
     if (is_ambiguous) {
     if (is_ambiguous) {
         if (from.file != ambiguous.file)
         if (from.file != ambiguous.file)
-            builder.append(from.to_algebraic().substring(0, 1));
+            builder.append(from.file_char());
         else if (from.rank != ambiguous.rank)
         else if (from.rank != ambiguous.rank)
-            builder.append(from.to_algebraic().substring(1, 1));
+            builder.append(from.rank_char());
         else
         else
             builder.append(from.to_algebraic());
             builder.append(from.to_algebraic());
     }
     }
 
 
     if (is_capture) {
     if (is_capture) {
         if (piece.type == Type::Pawn && !is_ambiguous)
         if (piece.type == Type::Pawn && !is_ambiguous)
-            builder.append(from.to_algebraic().substring(0, 1));
+            builder.append(from.file_char());
         builder.append('x');
         builder.append('x');
     }
     }
 
 

+ 4 - 0
Userland/Libraries/LibChess/Chess.h

@@ -1,5 +1,6 @@
 /*
 /*
  * Copyright (c) 2020, the SerenityOS developers.
  * Copyright (c) 2020, the SerenityOS developers.
+ * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  *
  *
  * SPDX-License-Identifier: BSD-2-Clause
  * 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 in_bounds() const { return rank >= 0 && file >= 0 && rank < 8 && file < 8; }
     bool is_light() const { return (rank % 2) != (file % 2); }
     bool is_light() const { return (rank % 2) != (file % 2); }
+
+    char file_char() const;
+    char rank_char() const;
     DeprecatedString to_algebraic() const;
     DeprecatedString to_algebraic() const;
 };
 };