string_buffer.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Author: [email protected] (Jonathan Tang)
  16. //
  17. #ifndef GUMBO_STRING_BUFFER_H_
  18. #define GUMBO_STRING_BUFFER_H_
  19. #include <stdbool.h>
  20. #include <stddef.h>
  21. #include "gumbo.h"
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. struct GumboInternalParser;
  26. // A struct representing a mutable, growable string. This consists of a
  27. // heap-allocated buffer that may grow (by doubling) as necessary. When
  28. // converting to a string, this allocates a new buffer that is only as long as
  29. // it needs to be. Note that the internal buffer here is *not* nul-terminated,
  30. // so be sure not to use ordinary string manipulation functions on it.
  31. typedef struct {
  32. // A pointer to the beginning of the string. NULL iff length == 0.
  33. char* data;
  34. // The length of the string fragment, in bytes. May be zero.
  35. size_t length;
  36. // The capacity of the buffer, in bytes.
  37. size_t capacity;
  38. } GumboStringBuffer;
  39. // Initializes a new GumboStringBuffer.
  40. void gumbo_string_buffer_init(
  41. struct GumboInternalParser* parser, GumboStringBuffer* output);
  42. // Ensures that the buffer contains at least a certain amount of space. Most
  43. // useful with snprintf and the other length-delimited string functions, which
  44. // may want to write directly into the buffer.
  45. void gumbo_string_buffer_reserve(struct GumboInternalParser* parser,
  46. size_t min_capacity, GumboStringBuffer* output);
  47. // Appends a single Unicode codepoint onto the end of the GumboStringBuffer.
  48. // This is essentially a UTF-8 encoder, and may add 1-4 bytes depending on the
  49. // value of the codepoint.
  50. void gumbo_string_buffer_append_codepoint(
  51. struct GumboInternalParser* parser, int c, GumboStringBuffer* output);
  52. // Appends a string onto the end of the GumboStringBuffer.
  53. void gumbo_string_buffer_append_string(struct GumboInternalParser* parser,
  54. GumboStringPiece* str, GumboStringBuffer* output);
  55. // Converts this string buffer to const char*, alloctaing a new buffer for it.
  56. char* gumbo_string_buffer_to_string(
  57. struct GumboInternalParser* parser, GumboStringBuffer* input);
  58. // Reinitialize this string buffer. This clears it by setting length=0. It
  59. // does not zero out the buffer itself.
  60. void gumbo_string_buffer_clear(
  61. struct GumboInternalParser* parser, GumboStringBuffer* input);
  62. // Deallocates this GumboStringBuffer.
  63. void gumbo_string_buffer_destroy(
  64. struct GumboInternalParser* parser, GumboStringBuffer* buffer);
  65. #ifdef __cplusplus
  66. }
  67. #endif
  68. #endif // GUMBO_STRING_BUFFER_H_