FreeCAD

Форк
0
/
MappedElement.cpp 
172 строки · 5.9 Кб
1
// SPDX-License-Identifier: LGPL-2.1-or-later
2

3
/***************************************************************************************************
4
 *                                                                                                 *
5
 *   Copyright (c) 2022 Zheng, Lei (realthunder) <realthunder.dev@gmail.com>                       *
6
 *   Copyright (c) 2023 FreeCAD Project Association                                                *
7
 *                                                                                                 *
8
 *   This file is part of FreeCAD.                                                                 *
9
 *                                                                                                 *
10
 *   FreeCAD is free software: you can redistribute it and/or modify it under the terms of the     *
11
 *   GNU Lesser General Public License as published by the Free Software Foundation, either        *
12
 *   version 2.1 of the License, or (at your option) any later version.                            *
13
 *                                                                                                 *
14
 *   FreeCAD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;          *
15
 *   without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     *
16
 *   See the GNU Lesser General Public License for more details.                                   *
17
 *                                                                                                 *
18
 *   You should have received a copy of the GNU Lesser General Public License along with           *
19
 *   FreeCAD. If not, see <https://www.gnu.org/licenses/>.                                         *
20
 *                                                                                                 *
21
 **************************************************************************************************/
22

23
// NOLINTNEXTLINE
24
#include "PreCompiled.h"
25

26
#ifndef _PreComp_
27
# include <cstdlib>
28
# include <unordered_set>
29
#endif
30

31
#include "DocumentObject.h"
32
#include "MappedElement.h"
33

34
using namespace Data;
35

36
bool ElementNameComparator::operator()(const MappedName& leftName,
37
                                       const MappedName& rightName) const
38
{
39
    int size = static_cast<int>(std::min(leftName.size(), rightName.size()));
40
    if (size == 0U) {
41
        return leftName.size() < rightName.size();
42
    }
43
    int currentIndex = 0;
44
    if (rightName[0] == '#') {
45
        if (leftName[0] != '#') {
46
            return true;
47
        }
48
        // If both string starts with '#', compare the following hex digits by
49
        // its integer value.
50
        int res = 0;
51
        for (currentIndex = 1; currentIndex < size; ++currentIndex) {
52
            auto ac = (unsigned char)leftName[currentIndex];
53
            auto bc = (unsigned char)rightName[currentIndex];
54
            if (std::isxdigit(bc) != 0) {
55
                if (std::isxdigit(ac) == 0) {
56
                    return true;
57
                }
58
                if (res == 0) {
59
                    if (ac < bc) {
60
                        res = -1;
61
                    }
62
                    else if (ac > bc) {
63
                        res = 1;
64
                    }
65
                }
66
            }
67
            else if (std::isxdigit(ac) != 0) {
68
                res = 1;
69
            }
70
            else {
71
                break;
72
            }
73
        }
74
        if (res < 0) {
75
            return true;
76
        }
77
        if (res > 0) {
78
            return false;
79
        }
80

81
        for (; currentIndex < size; ++currentIndex) {
82
            char ac = leftName[currentIndex];
83
            char bc = rightName[currentIndex];
84
            if (ac < bc) {
85
                return true;
86
            }
87
            if (ac > bc) {
88
                return false;
89
            }
90
        }
91
        return leftName.size() < rightName.size();
92
    }
93
    if (leftName[0] == '#') {
94
        return false;
95
    }
96

97
    // If the string does not start with '#', compare the non-digits prefix
98
    // using lexical order.
99
    for (currentIndex = 0; currentIndex < size; ++currentIndex) {
100
        auto ac = (unsigned char)leftName[currentIndex];
101
        auto bc = (unsigned char)rightName[currentIndex];
102
        if (std::isdigit(bc) == 0) {
103
            if (std::isdigit(ac) != 0) {
104
                return true;
105
            }
106
            if (ac < bc) {
107
                return true;
108
            }
109
            if (ac > bc) {
110
                return false;
111
            }
112
        }
113
        else if (std::isdigit(ac) == 0) {
114
            return false;
115
        }
116
        else {
117
            break;
118
        }
119
    }
120

121
    // Then compare the following digits part by integer value
122
    int res = 0;
123
    for (; currentIndex < size; ++currentIndex) {
124
        auto ac = (unsigned char)leftName[currentIndex];
125
        auto bc = (unsigned char)rightName[currentIndex];
126
        if (std::isdigit(bc) != 0) {
127
            if (std::isdigit(ac) == 0) {
128
                return true;
129
            }
130
            if (res == 0) {
131
                if (ac < bc) {
132
                    res = -1;
133
                }
134
                else if (ac > bc) {
135
                    res = 1;
136
                }
137
            }
138
        }
139
        else if (std::isdigit(ac) != 0) {
140
            return false;
141
        }
142
        else {
143
            break;
144
        }
145
    }
146
    if (res < 0) {
147
        return true;
148
    }
149
    if (res > 0) {
150
        return false;
151
    }
152

153
    // Finally, compare the remaining tail using lexical order
154
    for (; currentIndex < size; ++currentIndex) {
155
        char ac = leftName[currentIndex];
156
        char bc = rightName[currentIndex];
157
        if (ac < bc) {
158
            return true;
159
        }
160
        if (ac > bc) {
161
            return false;
162
        }
163
    }
164
    return leftName.size() < rightName.size();
165
}
166

167
HistoryItem::HistoryItem(App::DocumentObject *obj, const Data::MappedName &name)
168
    :obj(obj),tag(0),element(name)
169
{
170
    if(obj)
171
        tag = obj->getID();
172
}
173

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.