A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
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.
 
 
 
 

209 lines
4.8 KiB

  1. /*
  2. Copyright (C) 2012-2016 Ben Kurtovic <ben.kurtovic@gmail.com>
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of
  4. this software and associated documentation files (the "Software"), to deal in
  5. the Software without restriction, including without limitation the rights to
  6. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  7. of the Software, and to permit persons to whom the Software is furnished to do
  8. so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. */
  19. #include "textbuffer.h"
  20. #define INITIAL_CAPACITY 32
  21. #define RESIZE_FACTOR 2
  22. #define CONCAT_EXTRA 32
  23. /*
  24. Internal allocation function for textbuffers.
  25. */
  26. static int
  27. internal_alloc(Textbuffer *self, Py_UCS4 maxchar)
  28. {
  29. self->capacity = INITIAL_CAPACITY;
  30. self->length = 0;
  31. self->object = PyUnicode_New(self->capacity, maxchar);
  32. if (!self->object) {
  33. return -1;
  34. }
  35. self->kind = PyUnicode_KIND(self->object);
  36. self->data = PyUnicode_DATA(self->object);
  37. return 0;
  38. }
  39. /*
  40. Internal deallocation function for textbuffers.
  41. */
  42. static void
  43. internal_dealloc(Textbuffer *self)
  44. {
  45. Py_DECREF(self->object);
  46. }
  47. /*
  48. Internal resize function.
  49. */
  50. static int
  51. internal_resize(Textbuffer *self, Py_ssize_t new_cap)
  52. {
  53. PyObject *newobj;
  54. void *newdata;
  55. newobj = PyUnicode_New(new_cap, PyUnicode_MAX_CHAR_VALUE(self->object));
  56. if (!newobj) {
  57. return -1;
  58. }
  59. newdata = PyUnicode_DATA(newobj);
  60. memcpy(newdata, self->data, self->length * self->kind);
  61. Py_DECREF(self->object);
  62. self->object = newobj;
  63. self->data = newdata;
  64. self->capacity = new_cap;
  65. return 0;
  66. }
  67. /*
  68. Create a new textbuffer object.
  69. */
  70. Textbuffer *
  71. Textbuffer_new(TokenizerInput *text)
  72. {
  73. Textbuffer *self = malloc(sizeof(Textbuffer));
  74. Py_UCS4 maxchar = 0;
  75. maxchar = PyUnicode_MAX_CHAR_VALUE(text->object);
  76. if (!self) {
  77. goto fail_nomem;
  78. }
  79. if (internal_alloc(self, maxchar) < 0) {
  80. goto fail_dealloc;
  81. }
  82. return self;
  83. fail_dealloc:
  84. free(self);
  85. fail_nomem:
  86. PyErr_NoMemory();
  87. return NULL;
  88. }
  89. /*
  90. Deallocate the given textbuffer.
  91. */
  92. void
  93. Textbuffer_dealloc(Textbuffer *self)
  94. {
  95. internal_dealloc(self);
  96. free(self);
  97. }
  98. /*
  99. Reset a textbuffer to its initial, empty state.
  100. */
  101. int
  102. Textbuffer_reset(Textbuffer *self)
  103. {
  104. Py_UCS4 maxchar = 0;
  105. maxchar = PyUnicode_MAX_CHAR_VALUE(self->object);
  106. internal_dealloc(self);
  107. if (internal_alloc(self, maxchar)) {
  108. return -1;
  109. }
  110. return 0;
  111. }
  112. /*
  113. Write a Unicode codepoint to the given textbuffer.
  114. */
  115. int
  116. Textbuffer_write(Textbuffer *self, Py_UCS4 code)
  117. {
  118. if (self->length >= self->capacity) {
  119. if (internal_resize(self, self->capacity * RESIZE_FACTOR) < 0) {
  120. return -1;
  121. }
  122. }
  123. PyUnicode_WRITE(self->kind, self->data, self->length++, code);
  124. return 0;
  125. }
  126. /*
  127. Read a Unicode codepoint from the given index of the given textbuffer.
  128. This function does not check for bounds.
  129. */
  130. Py_UCS4
  131. Textbuffer_read(Textbuffer *self, Py_ssize_t index)
  132. {
  133. return PyUnicode_READ(self->kind, self->data, index);
  134. }
  135. /*
  136. Return the contents of the textbuffer as a Python Unicode object.
  137. */
  138. PyObject *
  139. Textbuffer_render(Textbuffer *self)
  140. {
  141. return PyUnicode_FromKindAndData(self->kind, self->data, self->length);
  142. }
  143. /*
  144. Concatenate the 'other' textbuffer onto the end of the given textbuffer.
  145. */
  146. int
  147. Textbuffer_concat(Textbuffer *self, Textbuffer *other)
  148. {
  149. Py_ssize_t newlen = self->length + other->length;
  150. if (newlen > self->capacity) {
  151. if (internal_resize(self, newlen + CONCAT_EXTRA) < 0) {
  152. return -1;
  153. }
  154. }
  155. assert(self->kind == other->kind);
  156. memcpy(((Py_UCS1 *) self->data) + self->kind * self->length,
  157. other->data,
  158. other->length * other->kind);
  159. self->length = newlen;
  160. return 0;
  161. }
  162. /*
  163. Reverse the contents of the given textbuffer.
  164. */
  165. void
  166. Textbuffer_reverse(Textbuffer *self)
  167. {
  168. Py_ssize_t i, end = self->length - 1;
  169. Py_UCS4 tmp;
  170. for (i = 0; i < self->length / 2; i++) {
  171. tmp = PyUnicode_READ(self->kind, self->data, i);
  172. PyUnicode_WRITE(
  173. self->kind, self->data, i, PyUnicode_READ(self->kind, self->data, end - i));
  174. PyUnicode_WRITE(self->kind, self->data, end - i, tmp);
  175. }
  176. }