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, 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 )
41 && wxIsdigit( str[cursor + 1] ) ) ) )
42 {
43 // number, possibly with sign
44 while( ++cursor < str.size() )
45 {
46 c = str[cursor];
47
48 if( wxIsdigit( c ) || c == 'v' || c == 'V' || c == '.' )
49 continue;
50 else
51 break;
52 }
53 }
54 else
55 {
56 while( ++cursor < str.size() )
57 {
58 c = str[cursor];
59
60 if( wxIsdigit( c ) )
61 break;
62 else
63 continue;
64 }
65 }
66
67 return str.substr( begin, cursor - begin );
68}
69
70
72{
73 wxString ret;
74
76
77 if( i == end() )
78 return ret;
79
80 const_iterator begin_of_range = i;
81 const_iterator last;
82
83 for( ;; )
84 {
85 last = i;
86 ++i;
87
88 int rc = ( i != end() ) ? Compare( *last, *i ) : -2;
89
90 assert( rc == -1 || rc == -2 );
91
92 if( rc == -1 )
93 // adjacent elements
94 continue;
95
96 ret += *begin_of_range;
97
98 if( begin_of_range != last )
99 {
100 ret += '-';
101 ret += *last;
102 }
103
104 if( i == end() )
105 break;
106
107 begin_of_range = i;
108 ret += ',';
109 }
110
111 return ret;
112}
113
114
116{
117 wxString ret;
118
119 for( const wxString& pinNumber : duplicate_pins )
120 {
121 ret += pinNumber;
122 ret += ',';
123 }
124
125 // Remove the trailing comma
126 ret.RemoveLast();
127
128 if( ret.IsEmpty() )
129 ret = _( "none" );
130
131 return ret;
132}
133
134
135int PIN_NUMBERS::Compare( const wxString& lhs, const wxString& rhs )
136{
137 wxString::size_type cursor1 = 0;
138 wxString::size_type cursor2 = 0;
139
140 wxString symbol1, symbol2;
141
142 for( ; ; )
143 {
144 symbol1 = getNextSymbol( lhs, cursor1 );
145 symbol2 = getNextSymbol( rhs, cursor2 );
146
147 if( symbol1.empty() && symbol2.empty() )
148 return 0;
149
150 if( symbol1.empty() )
151 return -2;
152
153 if( symbol2.empty() )
154 return 2;
155
156 bool sym1_isnumeric = symbol1.find_first_of( wxS( "0123456789" ) ) != wxString::npos;
157 bool sym2_isnumeric = symbol2.find_first_of( wxS( "0123456789" ) ) != wxString::npos;
158
159 if( sym1_isnumeric )
160 {
161 if( sym2_isnumeric )
162 {
163 // numeric comparison
164 wxString::size_type v1 = symbol1.find_first_of( wxS( "vV" ) );
165
166 if( v1 != wxString::npos )
167 symbol1[v1] = '.';
168
169 wxString::size_type v2 = symbol2.find_first_of( wxS( "vV" ) );
170
171 if( v2 != wxString::npos )
172 symbol2[v2] = '.';
173
174 double val1, val2;
175
176 symbol1.ToCDouble( &val1 );
177 symbol2.ToCDouble( &val2 );
178
179 if( val1 < val2 )
180 {
181 if( val1 == val2 - 1 )
182 return -1;
183 else
184 return -2;
185 }
186
187 if( val1 > val2 )
188 {
189 if( val1 == val2 + 1 )
190 return 1;
191 else
192 return 2;
193 }
194 }
195 else
196 {
197 return -2;
198 }
199 }
200 else
201 {
202 if( sym2_isnumeric )
203 return 2;
204
205 int res = symbol1.Cmp( symbol2 );
206
207 if( res != 0 )
208 return res;
209 }
210 }
211}
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:71
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)
VECTOR3I v1(5, 5, 5)
VECTOR3I res
VECTOR2I v2(1, 0)