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.

105 lines
3.5 KiB

  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, 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. #pragma once
  24. #include <optional>
  25. #include <wx/string.h>
  26. #include <kicommon.h>
  27. /**
  28. * Generic string incrementer.
  29. */
  30. KICOMMON_API bool IncrementString( wxString& aStr, int aDelta );
  31. /**
  32. * Heuristically increment a string's n'th part from the right.
  33. *
  34. * For example: incrementing the 0th part of A1 -> A2
  35. * 1st part of A1 -> B1
  36. *
  37. * This is a bit subjective as to what represents suitable
  38. * "incrementable" parts, but it tries to be smart about it.
  39. */
  40. class KICOMMON_API STRING_INCREMENTER
  41. {
  42. public:
  43. /**
  44. * If a alphabetic part is found, skip the letters I, O, S, Q, X, Z.
  45. * (if one is already there, increment it anyway).
  46. */
  47. void SetSkipIOSQXZ( bool aSkip ) { m_SkipIOSQXZ = aSkip; }
  48. /**
  49. * Set the maximum index for alphabetic parts.
  50. *
  51. * This means that if the index is greater than this, it will be treated
  52. * as un-incrementable. This is to avoid incrementing things like "TX" or
  53. * "CAN", which would be indexes of hundreds (unlikely to be a BGA row prefix,
  54. * for example).
  55. *
  56. * Setting < 0 disables the check (no limit)
  57. */
  58. void SetAlphabeticMaxIndex( int aMaxIndex ) { m_AlphabeticMaxIndex = aMaxIndex; }
  59. /**
  60. * Increment the n-th part from the right of the given string.
  61. */
  62. std::optional<wxString> Increment( const wxString& aStr, int aDelta, size_t aRightIndex ) const;
  63. private:
  64. enum class STRING_PART_TYPE
  65. {
  66. ALPHABETIC,
  67. INTEGER,
  68. SKIP,
  69. };
  70. bool incrementPart( wxString& aPart, STRING_PART_TYPE aType, int aDelta ) const;
  71. bool m_SkipIOSQXZ = true;
  72. int m_AlphabeticMaxIndex = 50;
  73. };
  74. /**
  75. * Attempt to convert a string to an integer, assuming it is an alphabetic
  76. * string like "A", "B", ... "Z", "AA", "AB", ... "ZZ", "AAA", ... in some
  77. * alphabet.
  78. *
  79. * @return The value of the string, or -1 if a character is
  80. * not in the alphabet.
  81. */
  82. KICOMMON_API int IndexFromAlphabetic( const wxString& aStr, const wxString& aAlphabet );
  83. /**
  84. * Get an alphabetic string like A, B, ... Z, AA, AB, ... ZZ, AAA, ...
  85. *
  86. * @param aIndex The index to convert.
  87. * @param aAlphabet The alphabet to use.
  88. * @param aZeroBasedNonUnitCols If true, cols other than the right most use the 0'th entry
  89. * (e.g. Z -> AA, not BA, but 9 -> 10, not 00).
  90. */
  91. KICOMMON_API wxString AlphabeticFromIndex( size_t aN, const wxString& aAlphabet,
  92. bool aZeroBasedNonUnitCols );