utf8.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. // This contains an implementation of a UTF8 iterator and decoder suitable for
  18. // an HTML5 parser. This does a bit more than straight UTF-8 decoding. The
  19. // HTML5 spec specifies that:
  20. // 1. Decoding errors are parse errors.
  21. // 2. Certain other codepoints (eg. control characters) are parse errors.
  22. // 3. Carriage returns and CR/LF groups are converted to line feeds.
  23. // http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#decoded-as-utf-8,-with-error-handling
  24. //
  25. // Also, we want to keep track of source positions for error handling. As a
  26. // result, we fold all that functionality into this decoder, and can't use an
  27. // off-the-shelf library.
  28. //
  29. // This header is internal-only, which is why we prefix functions with only
  30. // utf8_ or utf8_iterator_ instead of gumbo_utf8_.
  31. #ifndef GUMBO_UTF8_H_
  32. #define GUMBO_UTF8_H_
  33. #include <stdbool.h>
  34. #include <stddef.h>
  35. #include "gumbo.h"
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. struct GumboInternalError;
  40. struct GumboInternalParser;
  41. // Unicode replacement char.
  42. extern const int kUtf8ReplacementChar;
  43. typedef struct GumboInternalUtf8Iterator {
  44. // Points at the start of the code point most recently read into 'current'.
  45. const char* _start;
  46. // Points at the mark. The mark is initially set to the beginning of the
  47. // input.
  48. const char* _mark;
  49. // Points past the end of the iter, like a past-the-end iterator in the STL.
  50. const char* _end;
  51. // The code point under the cursor.
  52. int _current;
  53. // The width in bytes of the current code point.
  54. int _width;
  55. // The SourcePosition for the current location.
  56. GumboSourcePosition _pos;
  57. // The SourcePosition for the mark.
  58. GumboSourcePosition _mark_pos;
  59. // Pointer back to the GumboParser instance, for configuration options and
  60. // error recording.
  61. struct GumboInternalParser* _parser;
  62. } Utf8Iterator;
  63. // Returns true if this Unicode code point is in the list of characters
  64. // forbidden by the HTML5 spec, such as NUL bytes and undefined control chars.
  65. bool utf8_is_invalid_code_point(int c);
  66. // Initializes a new Utf8Iterator from the given byte buffer. The source does
  67. // not have to be NUL-terminated, but the length must be passed in explicitly.
  68. void utf8iterator_init(struct GumboInternalParser* parser, const char* source,
  69. size_t source_length, Utf8Iterator* iter);
  70. // Advances the current position by one code point.
  71. void utf8iterator_next(Utf8Iterator* iter);
  72. // Returns the current code point as an integer.
  73. int utf8iterator_current(const Utf8Iterator* iter);
  74. // Retrieves and fills the output parameter with the current source position.
  75. void utf8iterator_get_position(
  76. const Utf8Iterator* iter, GumboSourcePosition* output);
  77. // Retrieves a character pointer to the start of the current character.
  78. const char* utf8iterator_get_char_pointer(const Utf8Iterator* iter);
  79. // Retrieves a character pointer to 1 past the end of the buffer. This is
  80. // necessary for certain state machines and string comparisons that would like
  81. // to look directly for ASCII text in the buffer without going through the
  82. // decoder.
  83. const char* utf8iterator_get_end_pointer(const Utf8Iterator* iter);
  84. // If the upcoming text in the buffer matches the specified prefix (which has
  85. // length 'length'), consume it and return true. Otherwise, return false with
  86. // no other effects. If the length of the string would overflow the buffer,
  87. // this returns false. Note that prefix should not contain null bytes because
  88. // of the use of strncmp/strncasecmp internally. All existing use-cases adhere
  89. // to this.
  90. bool utf8iterator_maybe_consume_match(
  91. Utf8Iterator* iter, const char* prefix, size_t length, bool case_sensitive);
  92. // "Marks" a particular location of interest in the input stream, so that it can
  93. // later be reset() to. There's also the ability to record an error at the
  94. // point that was marked, as oftentimes that's more useful than the last
  95. // character before the error was detected.
  96. void utf8iterator_mark(Utf8Iterator* iter);
  97. // Returns the current input stream position to the mark.
  98. void utf8iterator_reset(Utf8Iterator* iter);
  99. // Sets the position and original text fields of an error to the value at the
  100. // mark.
  101. void utf8iterator_fill_error_at_mark(
  102. Utf8Iterator* iter, struct GumboInternalError* error);
  103. #ifdef __cplusplus
  104. }
  105. #endif
  106. #endif // GUMBO_UTF8_H_