MCPcopy Create free account
hub / github.com/dhbloo/rapfi / copyBoardTextFrom

Method copyBoardTextFrom

Rapfi/database/dbtypes.cpp:200–294  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

198}
199
200void DBRecord::copyBoardTextFrom(const DBRecord &rhs, bool overwrite)
201{
202 // If rhs has no board text, skip
203 if (!rhs.hasBoardText())
204 return;
205
206 // If this has no board text, copy directly from rhs
207 if (!hasBoardText()) {
208 size_t boardTextSegmentSize = std::min(rhs.text.find('\b'), rhs.text.size());
209 if (text.empty()) {
210 text = std::string_view {text.data(), boardTextSegmentSize};
211 }
212 else {
213 text.insert(0, rhs.text, 0, boardTextSegmentSize);
214 text.insert(boardTextSegmentSize, "\b");
215 }
216 }
217
218 // Merge board texts position by position
219 std::map<Pos, std::string> boardTextToMerge;
220
221 // Get all positions in rhs that have board text
222 {
223 size_t boardTextSegmentSize = std::min(rhs.text.find('\b'), rhs.text.size());
224 std::string_view boardTextSegment(rhs.text.data() + HeaderLength,
225 boardTextSegmentSize - HeaderLength);
226 for (std::string_view boardTextPair : split(boardTextSegment, "\n")) {
227 if (boardTextPair.size() > 2) {
228 Pos p {hexToCoord(boardTextPair[0]), hexToCoord(boardTextPair[1])};
229
230 // Remove extra '\0' due to bug in early implementation
231 if (boardTextPair.back() == '\0')
232 boardTextPair.remove_suffix(1);
233
234 boardTextToMerge.emplace(p, boardTextPair);
235 }
236 }
237 }
238
239 std::string boardTextSegment = BoardTextHeader;
240 size_t boardTextCount = 0;
241 // Get all positions in this and remove either one according to overwrite flag
242 {
243 size_t boardTextSegmentSize = std::min(text.find('\b'), text.size());
244 std::string_view oldBoardTextSegment(text.data() + HeaderLength,
245 boardTextSegmentSize - HeaderLength);
246
247 for (std::string_view boardTextPair : split(oldBoardTextSegment, "\n")) {
248 if (boardTextPair.size() <= 2)
249 continue;
250
251 Pos p {hexToCoord(boardTextPair[0]), hexToCoord(boardTextPair[1])};
252
253 // Remove extra '\0' due to bug in early implementation
254 if (boardTextPair.back() == '\0')
255 boardTextPair.remove_suffix(1);
256
257 if (auto it = boardTextToMerge.find(p); it != boardTextToMerge.end()) {

Callers 1

mergeDatabaseFunction · 0.80

Calls 11

splitFunction · 0.85
hexToCoordFunction · 0.85
hasBoardTextMethod · 0.80
findMethod · 0.45
sizeMethod · 0.45
emptyMethod · 0.45
insertMethod · 0.45
emplaceMethod · 0.45
endMethod · 0.45
eraseMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected