KiCad PCB EDA Suite
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 (C) 2015-2022 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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include "pin_numbers.h"
26#include <wx/crt.h>
27#include <wx/translation.h>
28
29
30wxString PIN_NUMBERS::getNextSymbol( const wxString& str, wxString::size_type& cursor )
31{
32 if( str.size() <= cursor )
33 return wxEmptyString;
34
35 wxString::size_type begin = cursor;
36
37 wxChar c = str[cursor];
38
39 // Need to check that there is a digit in the string before we parse it as a numeric
40 if( ( wxIsdigit( c ) || ( ( c == '+' || c == '-' ) && ( cursor < str.size() - 1 ) && wxIsdigit( str[cursor + 1] ) ) ) )
41 {
42 // number, possibly with sign
43 while( ++cursor < str.size() )
44 {
45 c = str[cursor];
46
47 if( wxIsdigit( c ) || c == 'v' || c == 'V' || c == '.' )
48 continue;
49 else
50 break;
51 }
52 }
53 else
54 {
55 while( ++cursor < str.size() )
56 {
57 c = str[cursor];
58
59 if( wxIsdigit( c ) )
60 break;
61 else
62 continue;
63 }
64 }
65
66 return str.substr( begin, cursor - begin );
67}
68
69
71{
72 wxString ret;
73
75 if( i == end() )
76 return ret;
77
78 const_iterator begin_of_range = i;
79 const_iterator last;
80
81 for( ;; )
82 {
83 last = i;
84 ++i;
85
86 int rc = ( i != end() ) ? Compare( *last, *i ) : -2;
87
88 assert( rc == -1 || rc == -2 );
89
90 if( rc == -1 )
91 // adjacent elements
92 continue;
93
94 ret += *begin_of_range;
95 if( begin_of_range != last )
96 {
97 ret += '-';
98 ret += *last;
99 }
100 if( i == end() )
101 break;
102 begin_of_range = i;
103 ret += ',';
104 }
105
106 return ret;
107}
108
109
111{
112 wxString ret;
113
114 for( const wxString& pinNumber : duplicate_pins )
115 {
116 ret += pinNumber;
117 ret += ',';
118 }
119
120 // Remove the trailing comma
121 ret.RemoveLast();
122
123 if( ret.IsEmpty() )
124 ret = _( "none" );
125
126 return ret;
127}
128
129
130int PIN_NUMBERS::Compare( const wxString& lhs, const wxString& rhs )
131{
132 wxString::size_type cursor1 = 0;
133 wxString::size_type cursor2 = 0;
134
135 wxString symbol1, symbol2;
136
137 for( ; ; )
138 {
139 symbol1 = getNextSymbol( lhs, cursor1 );
140 symbol2 = getNextSymbol( rhs, cursor2 );
141
142 if( symbol1.empty() && symbol2.empty() )
143 return 0;
144
145 if( symbol1.empty() )
146 return -2;
147
148 if( symbol2.empty() )
149 return 2;
150
151 bool sym1_isnumeric = symbol1.find_first_of( "0123456789" ) != wxString::npos;
152 bool sym2_isnumeric = symbol2.find_first_of( "0123456789" ) != wxString::npos;
153
154 if( sym1_isnumeric )
155 {
156 if( sym2_isnumeric )
157 {
158 // numeric comparison
159 wxString::size_type v1 = symbol1.find_first_of( "vV" );
160
161 if( v1 != wxString::npos )
162 symbol1[v1] = '.';
163
164 wxString::size_type v2 = symbol2.find_first_of( "vV" );
165
166 if( v2 != wxString::npos )
167 symbol2[v2] = '.';
168
169 double val1, val2;
170
171 symbol1.ToCDouble( &val1 );
172 symbol2.ToCDouble( &val2 );
173
174 if( val1 < val2 )
175 {
176 if( val1 == val2 - 1 )
177 return -1;
178 else
179 return -2;
180 }
181
182 if( val1 > val2 )
183 {
184 if( val1 == val2 + 1 )
185 return 1;
186 else
187 return 2;
188 }
189 }
190 else
191 return -2;
192 }
193 else
194 {
195 if( sym2_isnumeric )
196 return 2;
197
198 int res = symbol1.Cmp( symbol2 );
199
200 if( res != 0 )
201 return res;
202 }
203 }
204}
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)
Definition: pin_numbers.cpp:30
static int Compare(const wxString &lhs, const wxString &rhs)
wxString GetSummary() const
Definition: pin_numbers.cpp:70
iterator end()
Definition: pin_numbers.h:78
container_type::const_iterator const_iterator
Definition: pin_numbers.h:61
std::set< wxString > duplicate_pins
Definition: pin_numbers.h:84
iterator begin()
Definition: pin_numbers.h:77
#define _(s)
VECTOR2I v2(1, 0)
Test suite for KiCad math code.