| 198 | } |
| 199 | |
| 200 | void 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()) { |
no test coverage detected