1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-06-27 16:09:52 +00:00

Replace some std::string occurrences with std::string_view

std::string_view is more lightweight than std::string. Furthermore,
std::string_view variables can be declared constexpr.

closes https://github.com/official-stockfish/Stockfish/pull/4328

No functional change
This commit is contained in:
Sebastian Buchwald 2023-01-07 14:53:59 +01:00 committed by Joost VandeVondele
parent 5a88c5bb9b
commit e9e7a7b83f
4 changed files with 15 additions and 11 deletions

View file

@ -41,12 +41,13 @@ typedef WORD(*fun5_t)();
} }
#endif #endif
#include <cstdlib>
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <string_view>
#include <vector> #include <vector>
#include <cstdlib>
#if defined(__linux__) && !defined(__ANDROID__) #if defined(__linux__) && !defined(__ANDROID__)
#include <stdlib.h> #include <stdlib.h>
@ -68,7 +69,7 @@ namespace Stockfish {
namespace { namespace {
/// Version number or dev. /// Version number or dev.
const string version = "dev"; constexpr string_view version = "dev";
/// Our fancy logging facility. The trick here is to replace cin.rdbuf() and /// Our fancy logging facility. The trick here is to replace cin.rdbuf() and
/// cout.rdbuf() with two Tie objects that tie cin and cout to a file stream. We /// cout.rdbuf() with two Tie objects that tie cin and cout to a file stream. We
@ -151,13 +152,13 @@ string engine_info(bool to_uci) {
stringstream ss; stringstream ss;
ss << "Stockfish " << version << setfill('0'); ss << "Stockfish " << version << setfill('0');
if (version == "dev") if constexpr (version == "dev")
{ {
ss << "-"; ss << "-";
#ifdef GIT_DATE #ifdef GIT_DATE
ss << GIT_DATE; ss << GIT_DATE;
#else #else
const string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"); constexpr string_view months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
string month, day, year; string month, day, year;
stringstream date(__DATE__); // From compiler, format is "Sep 21 2008" stringstream date(__DATE__); // From compiler, format is "Sep 21 2008"

View file

@ -18,11 +18,12 @@
// Code for calculating NNUE evaluation function // Code for calculating NNUE evaluation function
#include <fstream>
#include <iomanip>
#include <iostream> #include <iostream>
#include <set> #include <set>
#include <sstream> #include <sstream>
#include <iomanip> #include <string_view>
#include <fstream>
#include "../evaluate.h" #include "../evaluate.h"
#include "../position.h" #include "../position.h"
@ -210,7 +211,7 @@ namespace Stockfish::Eval::NNUE {
return t; return t;
} }
static const std::string PieceToChar(" PNBRQK pnbrqk"); constexpr std::string_view PieceToChar(" PNBRQK pnbrqk");
// format_cp_compact() converts a Value into (centi)pawns and writes it in a buffer. // format_cp_compact() converts a Value into (centi)pawns and writes it in a buffer.

View file

@ -22,6 +22,7 @@
#include <cstring> // For std::memset, std::memcmp #include <cstring> // For std::memset, std::memcmp
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
#include <string_view>
#include "bitboard.h" #include "bitboard.h"
#include "misc.h" #include "misc.h"
@ -46,7 +47,7 @@ namespace Zobrist {
namespace { namespace {
const string PieceToChar(" PNBRQK pnbrqk"); constexpr std::string_view PieceToChar(" PNBRQK pnbrqk");
constexpr Piece Pieces[] = { W_PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING, constexpr Piece Pieces[] = { W_PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING,
B_PAWN, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING }; B_PAWN, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING };

View file

@ -24,9 +24,10 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <list> #include <list>
#include <sstream>
#include <type_traits>
#include <mutex> #include <mutex>
#include <sstream>
#include <string_view>
#include <type_traits>
#include "../bitboard.h" #include "../bitboard.h"
#include "../movegen.h" #include "../movegen.h"
@ -70,7 +71,7 @@ enum TBFlag { STM = 1, Mapped = 2, WinPlies = 4, LossPlies = 8, Wide = 16, Singl
inline WDLScore operator-(WDLScore d) { return WDLScore(-int(d)); } inline WDLScore operator-(WDLScore d) { return WDLScore(-int(d)); }
inline Square operator^(Square s, int i) { return Square(int(s) ^ i); } inline Square operator^(Square s, int i) { return Square(int(s) ^ i); }
const std::string PieceToChar = " PNBRQK pnbrqk"; constexpr std::string_view PieceToChar = " PNBRQK pnbrqk";
int MapPawns[SQUARE_NB]; int MapPawns[SQUARE_NB];
int MapB1H1H7[SQUARE_NB]; int MapB1H1H7[SQUARE_NB];