Implementations of the C++ Standard Library in... C. Can creatures armed without wings engage in flight?
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.

string.h 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * string.h of the C STL.
  3. *
  4. * Copyright (C) 2020 Luiserebii
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (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. #ifndef CSTL_STRING_H
  20. #define CSTL_STRING_H
  21. #include "vector.h"
  22. /**
  23. * Generate a set of vector functions for our string
  24. */
  25. // clang-format off
  26. declare_vector_class(string, char);
  27. // clang-format on
  28. /**
  29. * A value representing the amount of extra capacity to allocate.
  30. * In this case, it is meant to represent at least one for the
  31. * null terminator.
  32. *
  33. * Note that all allocation/reallocations therefore allocate
  34. * the capacity of the vector in terms of chars, + 1 (or,
  35. * STRING_CAPACITY_EXTRA).
  36. */
  37. #define STRING_CAPACITY_EXTRA 1
  38. /**
  39. * Initializes an empty string with the contents of the char* passed.
  40. * It is assumed that that char* is valid.
  41. */
  42. void string_init_cstr(string* str, const char* s);
  43. /**
  44. * Creates (allocates and initializes) an empty string with the contents of the char* passed.
  45. * It is assumed that that char* is valid.
  46. */
  47. string* string_create_cstr(const char* s);
  48. /**
  49. * Sets the contents of the string to the char* passed. If there is not enough space,
  50. * the string will expand to fit the string.
  51. */
  52. void string_asn_cstr(string* str, const char* s);
  53. /**
  54. * Concatenates the string src to the end of the string dest.
  55. */
  56. void string_cat(string* dest, const string* src);
  57. /**
  58. * Concatenates the char* string s to the end of the string str.
  59. */
  60. void string_cat_cstr(string* str, const char* s);
  61. /**
  62. * Compares string s1 to char* s2, and returns an integer representing
  63. * the result.
  64. *
  65. * If s1 < s2, then the result is a negative integer (i.e. n < 0).
  66. * If s1 > s2, then the result is a positive integer (i.e. n > 0).
  67. * If s1 == s2, then the result is 0.
  68. */
  69. int string_cmp_cstr(const string* s1, const char* s2);
  70. /**
  71. * Compares string s1 to string s2, and returns an integer representing
  72. * the result.
  73. *
  74. * If s1 < s2, then the result is a negative integer (i.e. n < 0).
  75. * If s1 > s2, then the result is a positive integer (i.e. n > 0).
  76. * If s1 == s2, then the result is 0.
  77. */
  78. int string_cmp(const string* s1, const string* s2);
  79. /**
  80. * Returns a pointer to the contents of the string as a null-terminated char.
  81. *
  82. * Note that if any of the elements of the string is a null-terminator, this will
  83. * result in a "chopped" c-string.
  84. *
  85. * Note that this function may increase the capacity of the string if needed.
  86. *
  87. * The pointer returned by this function may also become invalidated if any mutations
  88. * to the string occur - best to call once and use quickly before performing
  89. * any modifications.
  90. */
  91. const char* string_cstr(const string* str);
  92. #endif