17#ifndef HOOVER_CHESS_UTILS__PGN_READER__PGNREADER_STRING_UTILS_H_INCLUDED
18#define HOOVER_CHESS_UTILS__PGN_READER__PGNREADER_STRING_UTILS_H_INCLUDED
55template <std::
size_t t_maxLen>
58 static_assert(t_maxLen > 0U);
59 static_assert(t_maxLen <= 255U);
103 assert(length <= t_maxLen);
114 constexpr void assign(
const char *str)
noexcept
118 while (*str !=
'\0' && len < t_maxLen)
129 constexpr void assign(
const char *str, std::size_t strLen)
noexcept
131 const std::size_t len { std::min(
capacity(), strLen) };
133 for (std::size_t i { }; i < len; ++i)
144 constexpr inline char operator [] (std::size_t index)
const noexcept
146 assert(index <= t_maxLen);
166 assert(index <= t_maxLen);
174 constexpr inline char *
data() noexcept
183 constexpr inline const char *
data() const noexcept
192 static constexpr inline std::size_t
capacity() noexcept
200 constexpr inline std::size_t
size() const noexcept
252 return '1' +
rowOf(sq);
308 template <std::
size_t bufSize,
typename U
intRangeType,
typename U
intType>
311 static_assert(std::is_unsigned_v<UintType>);
312 static_assert(std::is_integral_v<UintType>);
313 static_assert(std::is_unsigned_v<UintRangeType>);
314 static_assert(std::is_integral_v<UintRangeType>);
315 static_assert(std::numeric_limits<UintRangeType>::digits10 + 1U == bufSize);
318 num &= std::numeric_limits<UintRangeType>::max();
324 *i++ =
'0' + (num % 10U);
335 const std::size_t len = i - s;
336 [[assume(len <= bufSize)]];
338 for (std::size_t c { }; c < len / 2U; ++c)
339 std::swap(s[c], s[len - c - 1U]);
357 char *i = ret.data();
385 (
static_cast<std::uint32_t
>(
'R') << 0
U) |
386 (
static_cast<std::uint32_t
>(
'Q') << 8U) |
387 (
static_cast<std::uint32_t
>(
'N') << 16U) |
388 (
static_cast<std::uint32_t
>(
'B') << 24U) };
390 unsigned int shift { (
static_cast<std::uint8_t
>(
promo) & 3U) * 8U };
444 (
static_cast<std::uint64_t
>(
'?') << 0
U) |
445 (
static_cast<std::uint64_t
>(
'N') << 16U) |
446 (
static_cast<std::uint64_t
>(
'B') << 24U) |
447 (
static_cast<std::uint64_t
>(
'R') << 32U) |
448 (
static_cast<std::uint64_t
>(
'Q') << 40U) |
449 (
static_cast<std::uint64_t
>(
'K') << 48U) };
454 const unsigned int index {
static_cast<unsigned int>(
p) };
The chessboard.
Definition chessboard.h:589
A Pascal-style length-prefixed string with the specified maximum length.
Definition pgnreader-string-utils.h:57
constexpr MiniString() noexcept
Initializing constructor. Sets the length of the contained string to 0.
Definition pgnreader-string-utils.h:69
std::array< char, t_maxLen > m_storage
Definition pgnreader-string-utils.h:63
constexpr void assign(const char *str) noexcept
Assigns a C-string.
Definition pgnreader-string-utils.h:114
constexpr void assign(const char *str, std::size_t strLen) noexcept
Assigns a string with length. In case the string is longer than the maximum capacity,...
Definition pgnreader-string-utils.h:129
constexpr char * data() noexcept
Returns a pointer to the string data. The string may be non-terminated by ('\0').
Definition pgnreader-string-utils.h:174
MiniString & operator=(const MiniString &) &=default
Copy assignment (default)
constexpr void setLength(std::uint8_t length) noexcept
Sets the length of the contained string without resetting the contents.
Definition pgnreader-string-utils.h:101
std::uint8_t m_length
Definition pgnreader-string-utils.h:62
~MiniString()=default
Destructor (default)
constexpr MiniString(MiniString_Uninitialized) noexcept
Non-initializing constructor. The state of the object is undefined.
Definition pgnreader-string-utils.h:75
constexpr const char * data() const noexcept
Returns a const pointer to the string data. The string may be non-terminated by ('\0').
Definition pgnreader-string-utils.h:183
MiniString(const MiniString &)=default
Copy constructor (default)
constexpr std::size_t size() const noexcept
Returns the size of the string.
Definition pgnreader-string-utils.h:200
constexpr std::string_view getStringView() const
Returns a string view to the string.
Definition pgnreader-string-utils.h:215
MiniString(MiniString &&)=default
Move constructor (default)
constexpr char operator[](std::size_t index) const noexcept
Returns a character at a specified offset. This function does not perform an index bounds check.
Definition pgnreader-string-utils.h:144
static constexpr std::size_t capacity() noexcept
Returns the capacity (maximum size) of a string to hold.
Definition pgnreader-string-utils.h:192
A legal move. Important: see the note!
Definition chessboard.h:198
Set of squares. Implemented using a bit-mask.
Definition chessboard-types-squareset.h:35
Miscellaneous string utilities.
Definition pgnreader-string-utils.h:235
static MiniString< 2U > sourceMaskToString(SquareSet srcMask) noexcept
Source mask to SAN notation.
static constexpr char colChar(Square sq) noexcept
Returns a file (column) designation character for a square, lower-case.
Definition pgnreader-string-utils.h:241
static MiniString< 7U > moveToSan(const ChessBoard &board, Move move)
Generates minimal SAN for a move. The move is fully validated by this function.
Definition pgnreader-string-utils.h:635
static constexpr MiniString< 1U > pieceToSanStr(Piece p) noexcept
Translates piece to SAN string.
Definition pgnreader-string-utils.h:433
static MiniString< 13U > plyNumToString(std::uint32_t plyNum) noexcept
Ply number to move string (e.g., "5." or "11...")
Definition pgnreader-string-utils.h:294
static constexpr char rowChar(Square sq) noexcept
Returns a rank (row) designation character for a square, lower-case.
Definition pgnreader-string-utils.h:250
static std::string_view pieceAndColorToString(PieceAndColor pc) noexcept
Returns a name string for PieceAndColor.
static constexpr char promoPieceChar(Piece promo) noexcept
Translates promotion piece to character (N/B/R/Q). Return value is unspecified for any other piece va...
Definition pgnreader-string-utils.h:377
static MiniString< 7U > moveToSanAndPlay(ChessBoard &board, Move move)
Generates minimal SAN for a move and plays it. The move is fully validated by this function.
static void boardToFEN(const ChessBoard &board, FenString &fen) noexcept
Builds a FEN string for the position on board.
static MiniString< 13U > moveNumToString(std::uint_fast32_t moveNum, Color turn) noexcept
Move number and side-to-move to move string (e.g., "5." or "11...")
Definition pgnreader-string-utils.h:351
static std::string_view moveTypeAndPromotionToString(MoveTypeAndPromotion typeAndPromotion) noexcept
Move type and promotion code to string.
static std::string_view squareToString(Square sq, std::string_view emptySquareName) noexcept
Returns a name string for Square.
static char * genUnsignedToString(char *s, UintType num) noexcept
Generic unsigned integer to string converter.
Definition pgnreader-string-utils.h:309
MoveTypeAndPromotion
Move type (4 bits, range: 0..15)
Definition chessboard.h:91
constexpr Color colorOfPly(std::uint_fast32_t plyNum) noexcept
Returns side to move for a ply number.
Definition pgnreader-types.h:66
constexpr RowColumn rowOf(Square sq) noexcept
Returns row number of square.
Definition chessboard-types.h:343
constexpr std::uint_fast32_t moveNumOfPly(std::uint_fast32_t plyNum) noexcept
Computes the full move for a ply number.
Definition pgnreader-types.h:75
PieceAndColor
Named piece and color.
Definition chessboard-types.h:219
Piece
Named piece.
Definition chessboard-types.h:204
Color
Color of a piece or side to move.
Definition chessboard-types.h:194
Square
Named square.
Definition chessboard-types.h:122
constexpr RowColumn columnOf(Square sq) noexcept
Returns column number of square.
Definition chessboard-types.h:331
@ NONE
Value representing no piece.
@ BLACK
Black piece or black side to move.
Definition chessboard-types-squareset.h:30
Tag for unitialized storage. This is a tiny optimization to save a single write when constructing a M...
Definition pgnreader-string-utils.h:42