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 #include <wx/crt.h>
27 
28 namespace {
29 
30 wxString GetNextComponent( 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  if( wxIsdigit( c ) || c == '+' || c == '-' )
40  {
41  // number, possibly with sign
42  while( ++cursor < str.size() )
43  {
44  c = str[cursor];
45 
46  if( wxIsdigit( c ) || c == 'v' || c == 'V' )
47  continue;
48  else
49  break;
50  }
51  }
52  else
53  {
54  while( ++cursor < str.size() )
55  {
56  c = str[cursor];
57 
58  if( wxIsdigit( c ) )
59  break;
60  else
61  continue;
62  }
63  }
64 
65  return str.substr( begin, cursor - begin );
66 }
67 
68 }
69 
70 
71 wxString PinNumbers::GetSummary() const
72 {
73  wxString ret;
74 
76  if( i == end() )
77  return ret;
78 
79  const_iterator begin_of_range = i;
80  const_iterator last;
81 
82  for( ;; )
83  {
84  last = i;
85  ++i;
86 
87  int rc = ( i != end() ) ? Compare( *last, *i ) : -2;
88 
89  assert( rc == -1 || rc == -2 );
90 
91  if( rc == -1 )
92  // adjacent elements
93  continue;
94 
95  ret += *begin_of_range;
96  if( begin_of_range != last )
97  {
98  ret += '-';
99  ret += *last;
100  }
101  if( i == end() )
102  break;
103  begin_of_range = i;
104  ret += ',';
105  }
106 
107  return ret;
108 }
109 
110 
111 int PinNumbers::Compare( const PinNumber& lhs, const PinNumber& rhs )
112 {
113  wxString::size_type cursor1 = 0;
114  wxString::size_type cursor2 = 0;
115 
116  wxString comp1, comp2;
117 
118  for( ; ; )
119  {
120  comp1 = GetNextComponent( lhs, cursor1 );
121  comp2 = GetNextComponent( rhs, cursor2 );
122 
123  if( comp1.empty() && comp2.empty() )
124  return 0;
125 
126  if( comp1.empty() )
127  return -2;
128 
129  if( comp2.empty() )
130  return 2;
131 
132  wxChar c1 = comp1[0];
133  wxChar c2 = comp2[0];
134 
135  if( wxIsdigit( c1 ) || c1 == '-' || c1 == '+' )
136  {
137  if( wxIsdigit( c2 ) || c2 == '-' || c2 == '+' )
138  {
139  // numeric comparison
140  wxString::size_type v1 = comp1.find_first_of( "vV" );
141 
142  if( v1 != wxString::npos )
143  comp1[v1] = '.';
144 
145  wxString::size_type v2 = comp2.find_first_of( "vV" );
146 
147  if( v2 != wxString::npos )
148  comp2[v2] = '.';
149 
150  double val1, val2;
151 
152  comp1.ToDouble( &val1 );
153  comp2.ToDouble( &val2 );
154 
155  if( val1 < val2 )
156  {
157  if( val1 == val2 - 1 )
158  return -1;
159  else
160  return -2;
161  }
162 
163  if( val1 > val2 )
164  {
165  if( val1 == val2 + 1 )
166  return 1;
167  else
168  return 2;
169  }
170  }
171  else
172  return -2;
173  }
174  else
175  {
176  if( wxIsdigit( c2 ) || c2 == '-' || c2 == '+' )
177  return 2;
178 
179  int res = comp1.Cmp( comp2 );
180 
181  if( res != 0 )
182  return res;
183  }
184  }
185 }
static int Compare(PinNumber const &lhs, PinNumber const &rhs)
Definition: pin_number.cpp:111
wxString GetSummary() const
Definition: pin_number.cpp:71
wxString PinNumber
Definition: pin_number.h:32
const string & str
Definition: json11.cpp:596
container_type::const_iterator const_iterator
Definition: pin_number.h:55
iterator begin()
Definition: pin_number.h:60
iterator end()
Definition: pin_number.h:61
YYCODETYPE lhs
size_t i
Definition: json11.cpp:597