]> git.sesse.net Git - stockfish/commitdiff
Replace some std::string occurrences with std::string_view
authorSebastian Buchwald <UniQP@web.de>
Sat, 7 Jan 2023 13:53:59 +0000 (14:53 +0100)
committerJoost VandeVondele <Joost.VandeVondele@gmail.com>
Mon, 9 Jan 2023 19:28:24 +0000 (20:28 +0100)
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

src/misc.cpp
src/nnue/evaluate_nnue.cpp
src/position.cpp
src/syzygy/tbprobe.cpp

index be7abba5d017ddae5f5068008f0b914fe8a45942..b651972bba18adff046b9668427059eff7dea925 100644 (file)
@@ -41,12 +41,13 @@ typedef WORD(*fun5_t)();
 }
 #endif
 
+#include <cstdlib>
 #include <fstream>
 #include <iomanip>
 #include <iostream>
 #include <sstream>
+#include <string_view>
 #include <vector>
-#include <cstdlib>
 
 #if defined(__linux__) && !defined(__ANDROID__)
 #include <stdlib.h>
@@ -68,7 +69,7 @@ namespace Stockfish {
 namespace {
 
 /// 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
 /// 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;
   ss << "Stockfish " << version << setfill('0');
 
-  if (version == "dev")
+  if constexpr (version == "dev")
   {
       ss << "-";
       #ifdef GIT_DATE
       ss << GIT_DATE;
       #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;
       stringstream date(__DATE__); // From compiler, format is "Sep 21 2008"
 
index 06281da06a5368508724ab3987b56848ee03ce94..f132de71385d01e6639f088d97c5a3884a0d6015 100644 (file)
 
 // Code for calculating NNUE evaluation function
 
+#include <fstream>
+#include <iomanip>
 #include <iostream>
 #include <set>
 #include <sstream>
-#include <iomanip>
-#include <fstream>
+#include <string_view>
 
 #include "../evaluate.h"
 #include "../position.h"
@@ -210,7 +211,7 @@ namespace Stockfish::Eval::NNUE {
     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.
index e82425af7c459ddebfe4d012d3f396be8a566f6d..cfd98f686e03ac21ef013437d91cb7d636fd24ef 100644 (file)
@@ -22,6 +22,7 @@
 #include <cstring> // For std::memset, std::memcmp
 #include <iomanip>
 #include <sstream>
+#include <string_view>
 
 #include "bitboard.h"
 #include "misc.h"
@@ -46,7 +47,7 @@ namespace Zobrist {
 
 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,
                              B_PAWN, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING };
index cc5e2852a09106d2c4b2a051cc5be6f61710d378..bbfd819d3fa933488cc6a008bc0770fb0df6a64a 100644 (file)
 #include <fstream>
 #include <iostream>
 #include <list>
+#include <mutex>
 #include <sstream>
+#include <string_view>
 #include <type_traits>
-#include <mutex>
 
 #include "../bitboard.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 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 MapB1H1H7[SQUARE_NB];