You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
4.4 KiB

  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. #include "pin_number.h"
  25. #include <wx/crt.h>
  26. namespace {
  27. wxString GetNextComponent( const wxString& str, wxString::size_type& cursor )
  28. {
  29. if( str.size() <= cursor )
  30. return wxEmptyString;
  31. wxString::size_type begin = cursor;
  32. wxChar c = str[cursor];
  33. if( wxIsdigit( c ) || c == '+' || c == '-' )
  34. {
  35. // number, possibly with sign
  36. while( ++cursor < str.size() )
  37. {
  38. c = str[cursor];
  39. if( wxIsdigit( c ) || c == 'v' || c == 'V' )
  40. continue;
  41. else
  42. break;
  43. }
  44. }
  45. else
  46. {
  47. while( ++cursor < str.size() )
  48. {
  49. c = str[cursor];
  50. if( wxIsdigit( c ) )
  51. break;
  52. else
  53. continue;
  54. }
  55. }
  56. return str.substr( begin, cursor - begin );
  57. }
  58. }
  59. wxString PinNumbers::GetSummary() const
  60. {
  61. wxString ret;
  62. const_iterator i = begin();
  63. if( i == end() )
  64. return ret;
  65. const_iterator begin_of_range = i;
  66. const_iterator last;
  67. for( ;; )
  68. {
  69. last = i;
  70. ++i;
  71. int rc = ( i != end() ) ? Compare( *last, *i ) : -2;
  72. assert( rc == -1 || rc == -2 );
  73. if( rc == -1 )
  74. // adjacent elements
  75. continue;
  76. ret += *begin_of_range;
  77. if( begin_of_range != last )
  78. {
  79. ret += '-';
  80. ret += *last;
  81. }
  82. if( i == end() )
  83. break;
  84. begin_of_range = i;
  85. ret += ',';
  86. }
  87. return ret;
  88. }
  89. int PinNumbers::Compare( const PinNumber& lhs, const PinNumber& rhs )
  90. {
  91. wxString::size_type cursor1 = 0;
  92. wxString::size_type cursor2 = 0;
  93. wxString comp1, comp2;
  94. for( ; ; )
  95. {
  96. comp1 = GetNextComponent( lhs, cursor1 );
  97. comp2 = GetNextComponent( rhs, cursor2 );
  98. if( comp1.empty() && comp2.empty() )
  99. return 0;
  100. if( comp1.empty() )
  101. return -2;
  102. if( comp2.empty() )
  103. return 2;
  104. wxChar c1 = comp1[0];
  105. wxChar c2 = comp2[0];
  106. if( wxIsdigit( c1 ) || c1 == '-' || c1 == '+' )
  107. {
  108. if( wxIsdigit( c2 ) || c2 == '-' || c2 == '+' )
  109. {
  110. // numeric comparison
  111. wxString::size_type v1 = comp1.find_first_of( "vV" );
  112. if( v1 != wxString::npos )
  113. comp1[v1] = '.';
  114. wxString::size_type v2 = comp2.find_first_of( "vV" );
  115. if( v2 != wxString::npos )
  116. comp2[v2] = '.';
  117. double val1, val2;
  118. comp1.ToCDouble( &val1 );
  119. comp2.ToCDouble( &val2 );
  120. if( val1 < val2 )
  121. {
  122. if( val1 == val2 - 1 )
  123. return -1;
  124. else
  125. return -2;
  126. }
  127. if( val1 > val2 )
  128. {
  129. if( val1 == val2 + 1 )
  130. return 1;
  131. else
  132. return 2;
  133. }
  134. }
  135. else
  136. return -2;
  137. }
  138. else
  139. {
  140. if( wxIsdigit( c2 ) || c2 == '-' || c2 == '+' )
  141. return 2;
  142. int res = comp1.Cmp( comp2 );
  143. if( res != 0 )
  144. return res;
  145. }
  146. }
  147. }