KiCad PCB EDA Suite
Loading...
Searching...
No Matches
refdes_utils.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 The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <refdes_utils.h>
21
22#include <string_utils.h>
23
24#include <algorithm>
25#include <cctype>
26
27
28namespace UTIL
29{
30
31wxString GetRefDesPrefix( const wxString& aRefDes )
32{
33 // find the first non-digit, non-question-mark character from the back
34 auto res = std::find_if( aRefDes.rbegin(), aRefDes.rend(),
35 []( wxUniChar aChr )
36 {
37 return aChr != '?' && !std::isdigit( aChr );
38 } );
39
40 return { aRefDes.begin(), res.base() };
41}
42
43
44wxString GetRefDesUnannotated( const wxString& aSource )
45{
46 return UTIL::GetRefDesPrefix( aSource ) + wxT( "?" );
47}
48
49
50int GetRefDesNumber( const wxString& aRefDes )
51{
52 int retval = -1; // negative to indicate not found
53 size_t firstnum = aRefDes.find_first_of( wxS( "0123456789" ) );
54
55 if( firstnum != wxString::npos )
56 {
57 wxString candidateValue = aRefDes.Mid( firstnum );
58 long result;
59
60 if( !candidateValue.ToLong( &result ) )
61 retval = -1;
62 else
63 retval = static_cast<int>( result );
64 }
65
66 return retval;
67}
68
69
70wxString FormatRefDesRanges( const std::vector<wxString>& aReferences, const wxString& aRefDelimiter,
71 const wxString& aRefRangeDelimiter )
72{
73 std::vector<std::pair<wxString, wxString>> bases;
74
75 for( const wxString& reference : aReferences )
76 bases.emplace_back( reference, wxEmptyString );
77
78 return FormatRefDesRanges( bases, aRefDelimiter, aRefRangeDelimiter );
79}
80
81
82wxString FormatRefDesRanges( const std::vector<std::pair<wxString, wxString>>& aBasesAndSuffixes,
83 const wxString& aRefDelimiter, const wxString& aRefRangeDelimiter )
84{
85 std::vector<std::pair<wxString, wxString>> sorted( aBasesAndSuffixes );
86
87 // A range must not span channels, so each suffix's references are made adjacent
88 std::vector<wxString> suffixOrder;
89
90 for( const auto& [base, suffix] : sorted )
91 {
92 if( std::find( suffixOrder.begin(), suffixOrder.end(), suffix ) == suffixOrder.end() )
93 suffixOrder.push_back( suffix );
94 }
95
96 std::stable_sort( sorted.begin(), sorted.end(),
97 [&]( const auto& a, const auto& b )
98 {
99 return std::find( suffixOrder.begin(), suffixOrder.end(), a.second )
100 < std::find( suffixOrder.begin(), suffixOrder.end(), b.second );
101 } );
102
103 auto composed =
104 [&]( size_t aIndex )
105 {
106 return sorted[aIndex].first + sorted[aIndex].second;
107 };
108
109 wxString retVal;
110 size_t i = 0;
111
112 while( i < sorted.size() )
113 {
114 const wxString& reference = sorted[i].first;
115 const wxString& suffix = sorted[i].second;
116 wxString prefix = GetRefDesPrefix( reference );
117 wxString numberText = reference.Mid( prefix.length() );
118 long number;
119 bool hasNumber = numberText.ToLong( &number );
120 size_t range = 1;
121
122 while( hasNumber && i + range < sorted.size() )
123 {
124 const wxString& nextReference = sorted[i + range].first;
125 wxString nextPrefix = GetRefDesPrefix( nextReference );
126 wxString nextNumberText = nextReference.Mid( nextPrefix.length() );
127 long nextNumber;
128
129 if( nextPrefix != prefix || sorted[i + range].second != suffix
130 || !nextNumberText.ToLong( &nextNumber ) || nextNumber != number + static_cast<long>( range ) )
131 {
132 break;
133 }
134
135 range++;
136
137 if( range == 2 && aRefRangeDelimiter.IsEmpty() )
138 break;
139 }
140
141 if( !retVal.IsEmpty() )
142 retVal << aRefDelimiter;
143
144 if( range == 1 )
145 {
146 retVal << composed( i );
147 }
148 else if( range == 2 || aRefRangeDelimiter.IsEmpty() )
149 {
150 retVal << composed( i );
151 retVal << aRefDelimiter;
152 retVal << composed( i + 1 );
153 }
154 else
155 {
156 retVal << composed( i );
157 retVal << aRefRangeDelimiter;
158 retVal << composed( i + range - 1 );
159 }
160
161 i += range;
162 }
163
164 return retVal;
165}
166
167} // namespace UTIL
A model subscriber implementation using links to represent connections.
wxString GetRefDesPrefix(const wxString &aRefDes)
Get the (non-numeric) prefix from a refdes - e.g.
wxString FormatRefDesRanges(const std::vector< wxString > &aReferences, const wxString &aRefDelimiter, const wxString &aRefRangeDelimiter)
Format a sorted list of reference designators, shortening consecutive sequences of three or more refe...
wxString GetRefDesUnannotated(const wxString &aSource)
Return an unannotated refdes from either a prefix or an existing refdes.
int GetRefDesNumber(const wxString &aRefDes)
Get the numeric suffix from a refdes - e.g.
Collection of utility functions for component reference designators (refdes)
nanodbc::result res
wxString result
Test unit parsing edge cases and error handling.