LCOV - code coverage report
Current view: top level - src/engraving/infrastructure - eid.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 91.7 % 121 111
Test Date: 2026-11-19 04:14:36

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-Studio-CLA-applies
       4              :  *
       5              :  * MuseScore Studio
       6              :  * Music Composition & Notation
       7              :  *
       8              :  * Copyright (C) 2021 MuseScore Limited and others
       9              :  *
      10              :  * This program is free software: you can redistribute it and/or modify
      11              :  * it under the terms of the GNU General Public License version 3 as
      12              :  * published by the Free Software Foundation.
      13              :  *
      14              :  * This program is distributed in the hope that it will be useful,
      15              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17              :  * GNU General Public License for more details.
      18              :  *
      19              :  * You should have received a copy of the GNU General Public License
      20              :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      21              :  */
      22              : #include "eid.h"
      23              : 
      24              : #include <string>
      25              : #include <random>
      26              : 
      27              : #include "global/log.h"
      28              : 
      29              : using namespace mu::engraving;
      30              : 
      31              : static constexpr char SEPARATOR = '_';
      32              : 
      33       214498 : static char* toBase64Chars(char* const first, char* const last, uint64_t n)
      34              : {
      35              :     static constexpr char CHARS[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
      36              : 
      37       214498 :     IF_ASSERT_FAILED(static_cast<size_t>(last - first) >= EID::MAX_UINT64_BASE64_SIZE) {
      38            0 :         return first;
      39              :     }
      40              : 
      41       214498 :     char* it = first;
      42              :     do {
      43       415454 :         *it++ = CHARS[n % 64];
      44       415454 :         n = n / 64;
      45       415454 :     } while (n != 0);
      46              : 
      47       214498 :     return it;
      48              : }
      49              : 
      50       498973 : static constexpr uint64_t charToInt(char c)
      51              : {
      52       498973 :     switch (c) {
      53         7080 :     case 'A': return 0;
      54        24943 :     case 'B': return 1;
      55        17423 :     case 'C': return 2;
      56        17077 :     case 'D': return 3;
      57        12643 :     case 'E': return 4;
      58         8924 :     case 'F': return 5;
      59         9136 :     case 'G': return 6;
      60         9396 :     case 'H': return 7;
      61         9539 :     case 'I': return 8;
      62         9194 :     case 'J': return 9;
      63         8879 :     case 'K': return 10;
      64         9093 :     case 'L': return 11;
      65         9083 :     case 'M': return 12;
      66         9049 :     case 'N': return 13;
      67         9297 :     case 'O': return 14;
      68         9567 :     case 'P': return 15;
      69         7422 :     case 'Q': return 16;
      70         6805 :     case 'R': return 17;
      71         6272 :     case 'S': return 18;
      72         6734 :     case 'T': return 19;
      73         7146 :     case 'U': return 20;
      74         7111 :     case 'V': return 21;
      75         6599 :     case 'W': return 22;
      76         6883 :     case 'X': return 23;
      77         6817 :     case 'Y': return 24;
      78         6518 :     case 'Z': return 25;
      79         6367 :     case 'a': return 26;
      80         6574 :     case 'b': return 27;
      81         6532 :     case 'c': return 28;
      82         6936 :     case 'd': return 29;
      83         6621 :     case 'e': return 30;
      84         6508 :     case 'f': return 31;
      85         6775 :     case 'g': return 32;
      86         6411 :     case 'h': return 33;
      87         6640 :     case 'i': return 34;
      88         6607 :     case 'j': return 35;
      89         6592 :     case 'k': return 36;
      90         6516 :     case 'l': return 37;
      91         6445 :     case 'm': return 38;
      92         6762 :     case 'n': return 39;
      93         6977 :     case 'o': return 40;
      94         6628 :     case 'p': return 41;
      95         6584 :     case 'q': return 42;
      96         6538 :     case 'r': return 43;
      97         6565 :     case 's': return 44;
      98         6439 :     case 't': return 45;
      99         6427 :     case 'u': return 46;
     100         6253 :     case 'v': return 47;
     101         6733 :     case 'w': return 48;
     102         6475 :     case 'x': return 49;
     103         6518 :     case 'y': return 50;
     104         6650 :     case 'z': return 51;
     105         6723 :     case '0': return 52;
     106         6559 :     case '1': return 53;
     107         6390 :     case '2': return 54;
     108         6790 :     case '3': return 55;
     109         6810 :     case '4': return 56;
     110         6608 :     case '5': return 57;
     111         6625 :     case '6': return 58;
     112         6257 :     case '7': return 59;
     113         6802 :     case '8': return 60;
     114         6270 :     case '9': return 61;
     115         6623 :     case '+': return 62;
     116         6813 :     case '/': return 63;
     117            0 :     default:
     118            0 :         UNREACHABLE;
     119              :         return uint64_t(-1);
     120              :     }
     121              : }
     122              : 
     123        93986 : static uint64_t base64StrToInt64(const std::string& s)
     124              : {
     125        93986 :     uint64_t result = 0;
     126              : 
     127       592959 :     for (auto iter = s.rbegin(); iter != s.rend(); ++iter) {
     128       498973 :         result = result << 6;
     129       498973 :         result |= charToInt(*iter);
     130              :     }
     131              : 
     132        93986 :     return result;
     133              : }
     134              : 
     135       107249 : char* EID::toChars(char* const first, char* const last) const
     136              : {
     137       107249 :     IF_ASSERT_FAILED(static_cast<size_t>(last - first) >= MAX_STR_SIZE) {
     138            0 :         return first;
     139              :     }
     140              : 
     141       107249 :     char* it = toBase64Chars(first, last, m_first);
     142       107249 :     *it++ = SEPARATOR;
     143       107249 :     it = toBase64Chars(it, last, m_second);
     144              : 
     145       107249 :     return it;
     146              : }
     147              : 
     148        12541 : std::string EID::toStdString() const
     149              : {
     150        12541 :     std::string base64Repr(MAX_STR_SIZE, char {});
     151        12541 :     const char* last = toChars(base64Repr.data(), base64Repr.data() + base64Repr.size());
     152        12541 :     base64Repr.resize(last - base64Repr.data());
     153              : 
     154        12541 :     return base64Repr;
     155            0 : }
     156              : 
     157        48583 : EID EID::fromStdString(const std::string& s)
     158              : {
     159        48583 :     std::stringstream ss(s);
     160        48583 :     std::string str;
     161        48583 :     std::vector<std::string> strings;
     162              : 
     163       144159 :     while (std::getline(ss, str, SEPARATOR)) {
     164        95576 :         strings.push_back(str);
     165              :     }
     166              : 
     167        48583 :     if (strings.size() != 2) {
     168         1590 :         return EID::invalid();
     169              :     }
     170              : 
     171        46993 :     const std::string& first = strings[0];
     172        46993 :     const std::string& second = strings[1];
     173              : 
     174        46993 :     return EID(base64StrToInt64(first), base64StrToInt64(second));
     175        48583 : }
     176              : 
     177        48582 : EID EID::fromStdString(const std::string_view& s)
     178              : {
     179        97164 :     return fromStdString(std::string(s));
     180              : }
     181              : 
     182            0 : EID EID::newUnique()
     183              : {
     184            0 :     static std::random_device s_device;
     185            0 :     thread_local static std::mt19937_64 s_engine(s_device());
     186            0 :     thread_local static std::uniform_int_distribution<uint64_t> s_unifDist;
     187              : 
     188            0 :     return EID(s_unifDist(s_engine), s_unifDist(s_engine));
     189              : }
     190              : 
     191              : // FOR UNIT TESTING
     192              : // EIDs are creates sequentially instead of randomly for test repeatability
     193              : 
     194        70395 : EID EID::newUniqueTestMode(uint64_t& maxVal)
     195              : {
     196        70395 :     ++maxVal;
     197        70395 :     return EID(maxVal, maxVal);
     198              : }
     199              : 
     200       110132 : void EID::updateMaxValTestMode(const EID& curEID, uint64_t& maxVal)
     201              : {
     202       110132 :     maxVal = std::max(maxVal, curEID.m_first);
     203       110132 :     maxVal = std::max(maxVal, curEID.m_second);
     204       110132 : }
        

Generated by: LCOV version 2.0-1