KiCad PCB EDA Suite
pin_number.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 KiCad Developers, see CHANGELOG.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_number.h"
26 
27 namespace {
28 
29 wxString GetNextComponent( const wxString& str, wxString::size_type& cursor )
30 {
31  if( str.size() <= cursor )
32  return wxEmptyString;
33 
34  wxString::size_type begin = cursor;
35 
36  wxUniChar c = str[cursor];
37 
38  if( isdigit( c ) || c == '+' || c == '-' )
39  {
40  // number, possibly with sign
41  while( ++cursor < str.size() )
42  {
43  c = str[cursor];
44 
45  if( isdigit( c ) || c == 'v' || c == 'V' )
46  continue;
47  else
48  break;
49  }
50  }
51  else
52  {
53  while( ++cursor < str.size() )
54  {
55  c = str[cursor];
56 
57  if( isdigit( c ) )
58  break;
59  else
60  continue;
61  }
62  }
63 
64  return str.substr( begin, cursor - begin );
65 }
66 
67 }
68 
69 
70 wxString PinNumbers::GetSummary() const
71 {
72  wxString ret;
73 
74  const_iterator i = begin();
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 
110 int PinNumbers::Compare( const PinNumber& lhs, const PinNumber& rhs )
111 {
112  wxString::size_type cursor1 = 0;
113  wxString::size_type cursor2 = 0;
114 
115  wxString comp1, comp2;
116 
117  for( ; ; )
118  {
119  comp1 = GetNextComponent( lhs, cursor1 );
120  comp2 = GetNextComponent( rhs, cursor2 );
121 
122  if( comp1.empty() && comp2.empty() )
123  return 0;
124 
125  if( comp1.empty() )
126  return -2;
127 
128  if( comp2.empty() )
129  return 2;
130 
131  wxUniChar c1 = comp1[0];
132  wxUniChar c2 = comp2[0];
133 
134  if( isdigit( c1 ) || c1 == '-' || c1 == '+' )
135  {
136  if( isdigit( c2 ) || c2 == '-' || c2 == '+' )
137  {
138  // numeric comparison
139  wxString::size_type v1 = comp1.find_first_of( "vV" );
140 
141  if( v1 != wxString::npos )
142  comp1[v1] = '.';
143 
144  wxString::size_type v2 = comp2.find_first_of( "vV" );
145 
146  if( v2 != wxString::npos )
147  comp2[v2] = '.';
148 
149  double val1, val2;
150 
151  comp1.ToDouble( &val1 );
152  comp2.ToDouble( &val2 );
153 
154  if( val1 < val2 )
155  {
156  if( val1 == val2 - 1 )
157  return -1;
158  else
159  return -2;
160  }
161 
162  if( val1 > val2 )
163  {
164  if( val1 == val2 + 1 )
165  return 1;
166  else
167  return 2;
168  }
169  }
170  else
171  return -2;
172  }
173  else
174  {
175  if( isdigit( c2 ) || c2 == '-' || c2 == '+' )
176  return 2;
177 
178  int res = comp1.Cmp( comp2 );
179 
180  if( res != 0 )
181  return res;
182  }
183  }
184 }
static int Compare(PinNumber const &lhs, PinNumber const &rhs)
Definition: pin_number.cpp:110
wxString PinNumber
Definition: pin_number.h:32
container_type::const_iterator const_iterator
Definition: pin_number.h:55
wxString GetSummary() const
Definition: pin_number.cpp:70
iterator begin()
Definition: pin_number.h:60
iterator end()
Definition: pin_number.h:61
YYCODETYPE lhs