KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pin_numbers.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2015 Simon Richter
5 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include "pin_numbers.h"
22#include <wx/crt.h>
23#include <wx/translation.h>
24
25
26wxString PIN_NUMBERS::getNextSymbol( const wxString& str, wxString::size_type& cursor )
27{
28 if( str.size() <= cursor )
29 return wxEmptyString;
30
31 wxString::size_type begin = cursor;
32
33 wxChar c = str[cursor];
34
35 // Need to check that there is a digit in the string before we parse it as a numeric
36 if( ( wxIsdigit( c ) || ( ( c == '+' || c == '-' ) && ( cursor < str.size() - 1 )
37 && wxIsdigit( str[cursor + 1] ) ) ) )
38 {
39 // number, possibly with sign
40 while( ++cursor < str.size() )
41 {
42 c = str[cursor];
43
44 if( wxIsdigit( c ) || c == 'v' || c == 'V' || c == '.' )
45 continue;
46 else
47 break;
48 }
49 }
50 else
51 {
52 while( ++cursor < str.size() )
53 {
54 c = str[cursor];
55
56 if( wxIsdigit( c ) )
57 break;
58 else
59 continue;
60 }
61 }
62
63 return str.substr( begin, cursor - begin );
64}
65
66
68{
69 wxString ret;
70
72
73 if( i == end() )
74 return ret;
75
76 const_iterator begin_of_range = i;
77 const_iterator last;
78
79 for( ;; )
80 {
81 last = i;
82 ++i;
83
84 int rc = ( i != end() ) ? Compare( *last, *i ) : -2;
85
86 assert( rc == -1 || rc == -2 );
87
88 if( rc == -1 )
89 // adjacent elements
90 continue;
91
92 ret += *begin_of_range;
93
94 if( begin_of_range != last )
95 {
96 ret += '-';
97 ret += *last;
98 }
99
100 if( i == end() )
101 break;
102
103 begin_of_range = i;
104 ret += ',';
105 }
106
107 return ret;
108}
109
110
112{
113 wxString ret;
114
115 for( const wxString& pinNumber : duplicate_pins )
116 {
117 ret += pinNumber;
118 ret += ',';
119 }
120
121 // Remove the trailing comma
122 ret.RemoveLast();
123
124 if( ret.IsEmpty() )
125 ret = _( "none" );
126
127 return ret;
128}
129
130
131int PIN_NUMBERS::Compare( const wxString& lhs, const wxString& rhs )
132{
133 wxString::size_type cursor1 = 0;
134 wxString::size_type cursor2 = 0;
135
136 wxString symbol1, symbol2;
137
138 for( ; ; )
139 {
140 symbol1 = getNextSymbol( lhs, cursor1 );
141 symbol2 = getNextSymbol( rhs, cursor2 );
142
143 if( symbol1.empty() && symbol2.empty() )
144 return 0;
145
146 if( symbol1.empty() )
147 return -2;
148
149 if( symbol2.empty() )
150 return 2;
151
152 bool sym1_isnumeric = symbol1.find_first_of( wxS( "0123456789" ) ) != wxString::npos;
153 bool sym2_isnumeric = symbol2.find_first_of( wxS( "0123456789" ) ) != wxString::npos;
154
155 if( sym1_isnumeric )
156 {
157 if( sym2_isnumeric )
158 {
159 // numeric comparison
160 wxString::size_type v1 = symbol1.find_first_of( wxS( "vV" ) );
161
162 if( v1 != wxString::npos )
163 symbol1[v1] = '.';
164
165 wxString::size_type v2 = symbol2.find_first_of( wxS( "vV" ) );
166
167 if( v2 != wxString::npos )
168 symbol2[v2] = '.';
169
170 double val1, val2;
171
172 symbol1.ToCDouble( &val1 );
173 symbol2.ToCDouble( &val2 );
174
175 if( val1 < val2 )
176 {
177 if( val1 == val2 - 1 )
178 return -1;
179 else
180 return -2;
181 }
182
183 if( val1 > val2 )
184 {
185 if( val1 == val2 + 1 )
186 return 1;
187 else
188 return 2;
189 }
190 }
191 else
192 {
193 return -2;
194 }
195 }
196 else
197 {
198 if( sym2_isnumeric )
199 return 2;
200
201 int res = symbol1.Cmp( symbol2 );
202
203 if( res != 0 )
204 return res;
205 }
206 }
207}
wxString GetDuplicates() const
Gets a formatted string of all the pins that have duplicate numbers.
static wxString getNextSymbol(const wxString &str, wxString::size_type &cursor)
static int Compare(const wxString &lhs, const wxString &rhs)
wxString GetSummary() const
iterator end()
Definition pin_numbers.h:73
container_type::const_iterator const_iterator
Definition pin_numbers.h:56
std::set< wxString > duplicate_pins
Definition pin_numbers.h:79
iterator begin()
Definition pin_numbers.h:72
#define _(s)
VECTOR3I v1(5, 5, 5)
VECTOR3I res
VECTOR2I v2(1, 0)