HooverChessUtils_PgnReader 0.9.0
Loading...
Searching...
No Matches
Static Public Member Functions | List of all members
hoover_chess_utils::pgn_reader::StringUtils Class Reference

Miscellaneous string utilities. More...

#include <pgnreader-string-utils.h>

Static Public Member Functions

static constexpr char colChar (Square sq) noexcept
 Returns a file (column) designation character for a square, lower-case.
 
static constexpr char rowChar (Square sq) noexcept
 Returns a rank (row) designation character for a square, lower-case.
 
static MiniString< 2UsourceMaskToString (SquareSet srcMask) noexcept
 Source mask to SAN notation.
 
static MiniString< 13UplyNumToString (std::uint32_t plyNum) noexcept
 Ply number to move string (e.g., "5." or "11...")
 
template<std::size_t bufSize, typename UintRangeType , typename UintType >
static char * genUnsignedToString (char *s, UintType num) noexcept
 Generic unsigned integer to string converter.
 
static MiniString< 13UmoveNumToString (std::uint_fast32_t moveNum, Color turn) noexcept
 Move number and side-to-move to move string (e.g., "5." or "11...")
 
static constexpr char promoPieceChar (Piece promo) noexcept
 Translates promotion piece to character (N/B/R/Q). Return value is unspecified for any other piece values.
 
static constexpr MiniString< 1UpieceToSanStr (Piece p) noexcept
 Translates piece to SAN string.
 
static MiniString< 7UmoveToSanAndPlay (ChessBoard &board, Move move)
 Generates minimal SAN for a move and plays it. The move is fully validated by this function.
 
static MiniString< 7UmoveToSan (const ChessBoard &board, Move move)
 Generates minimal SAN for a move. The move is fully validated by this function.
 
static std::string_view pieceAndColorToString (PieceAndColor pc) noexcept
 Returns a name string for PieceAndColor.
 
static std::string_view squareToString (Square sq, std::string_view emptySquareName) noexcept
 Returns a name string for Square.
 
static std::string_view moveTypeAndPromotionToString (MoveTypeAndPromotion typeAndPromotion) noexcept
 Move type and promotion code to string.
 
static void boardToFEN (const ChessBoard &board, FenString &fen) noexcept
 Builds a FEN string for the position on board.
 

Detailed Description

Miscellaneous string utilities.

Member Function Documentation

◆ boardToFEN()

static void hoover_chess_utils::pgn_reader::StringUtils::boardToFEN ( const ChessBoard board,
FenString fen 
)
staticnoexcept

Builds a FEN string for the position on board.

Parameters
[in]boardBoard that holds the position
[in]fenFEN for the position

◆ colChar()

static constexpr char hoover_chess_utils::pgn_reader::StringUtils::colChar ( Square  sq)
inlinestaticconstexprnoexcept

Returns a file (column) designation character for a square, lower-case.

Parameters
[in]sqValid square
Returns
Lower case file designation ('a''h')

◆ genUnsignedToString()

template<std::size_t bufSize, typename UintRangeType , typename UintType >
static char * hoover_chess_utils::pgn_reader::StringUtils::genUnsignedToString ( char *  s,
UintType  num 
)
inlinestaticnoexcept

Generic unsigned integer to string converter.

Template Parameters
bufSizeReserved buffer size. Must be exactly the maximum string size for the integer type
UintTypeUnsigned integer type
Parameters
[out]sPointer to receive the string
[in]numNumber to convert
Returns
Pointer one past the string. Difference to s is the length of the written string.

◆ moveNumToString()

static MiniString< 13U > hoover_chess_utils::pgn_reader::StringUtils::moveNumToString ( std::uint_fast32_t  moveNum,
Color  turn 
)
inlinestaticnoexcept

Move number and side-to-move to move string (e.g., "5." or "11...")

Parameters
[in]moveNumMove number
[in]turnSide to move
Returns
Move string in PGN move number format. E.g., "1." for first white move and "2..." for second black move.

◆ moveToSan()

static MiniString< 7U > hoover_chess_utils::pgn_reader::StringUtils::moveToSan ( const ChessBoard board,
Move  move 
)
inlinestatic

Generates minimal SAN for a move. The move is fully validated by this function.

Parameters
[in]boardChess board
[in]moveMove to play
Exceptions
PgnError(PgnErrorCode::ILLEGAL_MOVE)Illegal move

This function is implemented using moveToSanAndPlay(). In case the intention is to produce SAN notation while replaying the moves, it is faster to invoke moveToSanAndPlay() to combine the activities than calling this function and ChessBoard::doMove() separately.

See also
moveToSanAndPlay() for full specification.

◆ moveToSanAndPlay()

static MiniString< 7U > hoover_chess_utils::pgn_reader::StringUtils::moveToSanAndPlay ( ChessBoard board,
Move  move 
)
static

Generates minimal SAN for a move and plays it. The move is fully validated by this function.

Parameters
[in]boardChess board
[in]moveMove to play
Exceptions
PgnError(PgnErrorCode::ILLEGAL_MOVE)Illegal move. board is not modified.
std::logic_errorBad move type board is not modified.

Format of the produced move string is as follows:

SAN_MOVE = move_body ('+' | '#')?

where move_body consists the piece identifier and source/destination identifiers, appended with check/checkmate indicators as necessary.

move_body has the following format:

Type (MoveTypeAndPromotion) Condition (first match) Format Example
MoveTypeAndPromotion::REGULAR_PAWN_MOVE No capture <dest_file> <dest_rank> e4
MoveTypeAndPromotion::REGULAR_PAWN_MOVE Capture <src_file> 'x' <dest_file> <dest_rank> exc6
cxd6
MoveTypeAndPromotion::EN_PASSANT None <src_file> 'x' <dest_file> <dest_rank> exc5
cxd6
MoveTypeAndPromotion::REGULAR_KNIGHT_MOVE
MoveTypeAndPromotion::REGULAR_BISHOP_MOVE
MoveTypeAndPromotion::REGULAR_ROOK_MOVE
MoveTypeAndPromotion::REGULAR_QUEEN_MOVE
No disambiguation required <piece> <dest_file> <dest_rank> Ne4
<piece> <dest_file> 'x' <dest_rank> Nxf6
Source file disambiguation required <piece> <src_file> <dest_file> <dest_rank> Nbd2
<piece> <src_file> 'x' <dest_file> <dest_rank> Nexg5
Source rank disambiguation required <piece> <src_rank> <dest_file> <dest_rank> R1h3
<piece> <src_rank> 'x' <dest_file> <dest_rank> N5xd4
Source square required <piece> <src_file> <src_rank> <dest_file> <dest_rank> Qd4d5
<piece> <src_file> 'x' <src_rank> <dest_file> <dest_rank> Qc2xc5
MoveTypeAndPromotion::REGULAR_KING_MOVE No capture 'K' <dest_file> <dest_rank> Kd2
Capture "Kx" <dest_file> <dest_rank> Kxf7
MoveTypeAndPromotion::CASTLING_SHORT None "O-O" O-O
MoveTypeAndPromotion::CASTLING_LONG None "O-O-O" O-O-O
MoveTypeAndPromotion::PROMO_KNIGHT
MoveTypeAndPromotion::PROMO_BISHOP
MoveTypeAndPromotion::PROMO_ROOK
MoveTypeAndPromotion::PROMO_QUEEN
No capture <dest_file> <dest_rank> '=' <piece> a8=Q
Capture <src_file> 'x' <dest_file> <dest_rank> '=' <piece> fxg1=N

Disambiguation requirements:

Disambiguation requirement Description
None There is only a single legal move for the piece and destination.
Source file Two or more legal moves match with the piece and destination square. Specifying source file makes the move unambiguous.
Source rank Two or more legal moves match with the piece and destination square. Specifying source rank makes the move unambiguous.
Source square More than one legal move matches with the piece and destination square. Neither source file nor source rank is sufficient to make the move unambiguous.

Note that the potential check/checkmate indicator is not used for disambiguation.

Remarks
The maximum length of the returned move is 7 characters. Example: "Na1xb3+"

◆ moveTypeAndPromotionToString()

static std::string_view hoover_chess_utils::pgn_reader::StringUtils::moveTypeAndPromotionToString ( MoveTypeAndPromotion  typeAndPromotion)
staticnoexcept

Move type and promotion code to string.

Parameters
[in]typeAndPromotionMove type and promotion piece
Returns
String matching the enum value label or "??"

◆ pieceAndColorToString()

static std::string_view hoover_chess_utils::pgn_reader::StringUtils::pieceAndColorToString ( PieceAndColor  pc)
staticnoexcept

Returns a name string for PieceAndColor.

Parameters
[in]pcPieceAndColor. May be valid or invalid
Returns
Name string

For all concrete PieceAndColor values, the name is a two-letter color (lower-case 'w' or 'b') and piece (upper-case 'P', 'N', 'B', 'R', 'Q', 'K') string. For none values, the return string is two spaces. For any other values, the returned string is "??".

◆ pieceToSanStr()

static constexpr MiniString< 1U > hoover_chess_utils::pgn_reader::StringUtils::pieceToSanStr ( Piece  p)
inlinestaticconstexprnoexcept

Translates piece to SAN string.

Parameters
[in]pPiece
Returns
Piece string for SAN
Piece Returned string
Piece::PAWN Empty string
Piece::KNIGHT "N"
Piece::BISHOP "B"
Piece::ROOK "R"
Piece::QUEEN "Q"
Piece::KING "K"
Other "?"

◆ plyNumToString()

static MiniString< 13U > hoover_chess_utils::pgn_reader::StringUtils::plyNumToString ( std::uint32_t  plyNum)
inlinestaticnoexcept

Ply number to move string (e.g., "5." or "11...")

Parameters
[in]plyNumPly number
Returns
Move string in PGN move number format. E.g., "1." for first white move and "2..." for second black move.

◆ promoPieceChar()

static constexpr char hoover_chess_utils::pgn_reader::StringUtils::promoPieceChar ( Piece  promo)
inlinestaticconstexprnoexcept

Translates promotion piece to character (N/B/R/Q). Return value is unspecified for any other piece values.

Parameters
[in]promoPromotion piece
Returns
Piece character, one of (N/B/R/Q)

◆ rowChar()

static constexpr char hoover_chess_utils::pgn_reader::StringUtils::rowChar ( Square  sq)
inlinestaticconstexprnoexcept

Returns a rank (row) designation character for a square, lower-case.

Parameters
[in]sqValid square
Returns
Rank designation ('1''8')

◆ sourceMaskToString()

static MiniString< 2U > hoover_chess_utils::pgn_reader::StringUtils::sourceMaskToString ( SquareSet  srcMask)
staticnoexcept

Source mask to SAN notation.

Parameters
[in]srcMaskSource mask derived from SAN notation
Returns
SAN notation for source square
Source mask Returned string
SquareSet::all() Empty string (no source mask was specified)
SquareSet::column(N) File designation character (source column was specified)
SquareSet::row(N) Rank designation character (source row was specified)
SquareSet::square(sq) File and rank designation characters (both source column and row were specified)
Other Unspecified but not undefined behavior

◆ squareToString()

static std::string_view hoover_chess_utils::pgn_reader::StringUtils::squareToString ( Square  sq,
std::string_view  emptySquareName 
)
staticnoexcept

Returns a name string for Square.

Parameters
[in]sqSquare. May be valid or invalid
[in]emptySquareNameName for Square::NONE and invalid squares.
Returns
Name string

For all valid squares, the name is a two-letter file and rank. For example: "a1", "h8".


The documentation for this class was generated from the following file: