KiCad PCB EDA Suite
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 (C) 2019 KiCad Developers, see CHANGELOG.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, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <refdes_utils.h>
25 
26 #include <kicad_string.h>
27 
28 #include <algorithm>
29 #include <cctype>
30 
31 
32 namespace UTIL
33 {
34 
35 wxString GetReferencePrefix( const wxString& aRefDes )
36 {
37  // find the first non-digit character from the back
38  auto res = std::find_if( aRefDes.rbegin(), aRefDes.rend(),
39  []( wxUniChar aChr ) { return !std::isdigit( aChr ); } );
40 
41  return { aRefDes.begin(), res.base() };
42 }
43 
44 
45 int RefDesStringCompare( const wxString& aFirst, const wxString& aSecond )
46 {
47  // Compare unescaped text
48  wxString strFWord = UnescapeString( aFirst );
49  wxString strSWord = UnescapeString( aSecond );
50 
51  // The different sections of the two strings
52  wxString strFWordBeg, strFWordMid, strFWordEnd;
53  wxString strSWordBeg, strSWordMid, strSWordEnd;
54 
55  // Split the two strings into separate parts
56  SplitString( strFWord, &strFWordBeg, &strFWordMid, &strFWordEnd );
57  SplitString( strSWord, &strSWordBeg, &strSWordMid, &strSWordEnd );
58 
59  // Compare the Beginning section of the strings
60  int isEqual = strFWordBeg.CmpNoCase( strSWordBeg );
61 
62  if( isEqual > 0 )
63  return 1;
64  else if( isEqual < 0 )
65  return -1;
66  else
67  {
68  // If the first sections are equal compare their digits
69  long lFirstDigit = 0;
70  long lSecondDigit = 0;
71 
72  strFWordMid.ToLong( &lFirstDigit );
73  strSWordMid.ToLong( &lSecondDigit );
74 
75  if( lFirstDigit > lSecondDigit )
76  return 1;
77  else if( lFirstDigit < lSecondDigit )
78  return -1;
79  // If the first two sections are equal compare the endings
80  else
81  return strFWordEnd.CmpNoCase( strSWordEnd );
82  }
83 }
84 
85 
86 } // namespace UTIL
int SplitString(wxString strToSplit, wxString *strBeginning, wxString *strDigits, wxString *strEnd)
Breaks a string into three parts: he alphabetic preamble, the numeric part, and any alphabetic ending...
Definition: string.cpp:577
Collection of utility functions for component reference designators (refdes)
int RefDesStringCompare(const wxString &aFirst, const wxString &aSecond)
Acts just like the strcmp function but treats numbers within the string text correctly for sorting.
wxString GetReferencePrefix(const wxString &aRefDes)
Get the (non-numeric) prefix from a refdes - e.g.
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:123