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.

119 lines
3.2KB

  1. // Copyright 2009 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // Utility functions for string processing.
  19. #ifndef AVRLIB_STRING_H_
  20. #define AVRLIB_STRING_H_
  21. #include "avrlib/base.h"
  22. #include <string.h>
  23. namespace avrlib {
  24. size_t strnlen(const char* string, size_t maxlen);
  25. void AlignRight(char* source, uint8_t width);
  26. void AlignLeft(char* source, uint8_t width);
  27. void PadRight(char* source, uint8_t width, char character);
  28. template<typename T>
  29. struct TypeInfo {
  30. enum {
  31. has_sign = 0,
  32. max_size = 5
  33. };
  34. };
  35. template<> struct TypeInfo<uint8_t> { enum { has_sign = 0, max_size = 3 }; };
  36. template<> struct TypeInfo<int8_t> { enum { has_sign = 1, max_size = 4 }; };
  37. template<> struct TypeInfo<uint16_t> { enum { has_sign = 0, max_size = 5 }; };
  38. template<> struct TypeInfo<int16_t> { enum { has_sign = 1, max_size = 6 }; };
  39. template<> struct TypeInfo<uint32_t> { enum { has_sign = 0, max_size = 10 }; };
  40. template<> struct TypeInfo<int32_t> { enum { has_sign = 1, max_size = 11 }; };
  41. static inline uint8_t NibbleToAscii(uint8_t digit) {
  42. return digit < 10 ? digit + 48 : digit + 87;
  43. }
  44. template<typename T>
  45. void Itoa(T i, uint8_t width, char* destination) {
  46. unsigned char digits[TypeInfo<T>::max_size + 1];
  47. if (width == 0) {
  48. return;
  49. }
  50. if (i == 0) {
  51. *destination++ = '0';
  52. width--;
  53. } else {
  54. if (TypeInfo<T>::has_sign && i < 0) {
  55. *destination++ = '-';
  56. width--;
  57. i = -i;
  58. }
  59. uint8_t digit = 0;
  60. while (i > 0) {
  61. digits[digit++] = i % 10;
  62. i /= 10;
  63. }
  64. while (digit) {
  65. *destination++ = 48 + digits[--digit];
  66. width--;
  67. }
  68. }
  69. if (width) {
  70. *destination++ = '\0';
  71. }
  72. }
  73. // A version of Itoa that does not allocate anything on the stack, and use
  74. // a fixed chunk of memory for reversing the digits. Caveat: if two Itoa
  75. // operations are taking place simultaneously, the results will be mixed.
  76. template<typename T>
  77. void UnsafeItoa(T i, uint8_t width, char* destination) {
  78. static unsigned char digits[TypeInfo<T>::max_size + 1];
  79. if (width == 0) {
  80. return;
  81. }
  82. if (i == 0) {
  83. *destination++ = '0';
  84. width--;
  85. } else {
  86. if (TypeInfo<T>::has_sign && i < 0) {
  87. *destination++ = '-';
  88. width--;
  89. i = -i;
  90. }
  91. uint8_t digit = 0;
  92. while (i > 0) {
  93. digits[digit++] = i % 10;
  94. i /= 10;
  95. }
  96. while (digit) {
  97. *destination++ = 48 + digits[--digit];
  98. width--;
  99. }
  100. }
  101. if (width) {
  102. *destination++ = '\0';
  103. }
  104. }
  105. } // namespace avrlib
  106. #endif // AVRLIB_STRING_H_