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.
 
 
 
 

233 lines
5.6 KiB

  1. /*
  2. Copyright (C) 2012-2015 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 internal_alloc(Textbuffer* self, Unicode maxchar)
  27. {
  28. self->capacity = INITIAL_CAPACITY;
  29. self->length = 0;
  30. #ifdef PEP_393
  31. self->object = PyUnicode_New(self->capacity, maxchar);
  32. if (!self->object)
  33. return -1;
  34. self->kind = PyUnicode_KIND(self->object);
  35. self->data = PyUnicode_DATA(self->object);
  36. #else
  37. (void) maxchar; // Unused
  38. self->data = malloc(sizeof(Unicode) * self->capacity);
  39. if (!self->data)
  40. return -1;
  41. #endif
  42. return 0;
  43. }
  44. /*
  45. Internal deallocation function for textbuffers.
  46. */
  47. static void internal_dealloc(Textbuffer* self)
  48. {
  49. #ifdef PEP_393
  50. Py_DECREF(self->object);
  51. #else
  52. free(self->data);
  53. #endif
  54. }
  55. /*
  56. Internal resize function.
  57. */
  58. static int internal_resize(Textbuffer* self, Py_ssize_t new_cap)
  59. {
  60. #ifdef PEP_393
  61. PyObject *newobj;
  62. void *newdata;
  63. newobj = PyUnicode_New(new_cap, PyUnicode_MAX_CHAR_VALUE(self->object));
  64. if (!newobj)
  65. return -1;
  66. newdata = PyUnicode_DATA(newobj);
  67. memcpy(newdata, self->data, self->length * self->kind);
  68. Py_DECREF(self->object);
  69. self->object = newobj;
  70. self->data = newdata;
  71. #else
  72. if (!(self->data = realloc(self->data, sizeof(Unicode) * new_cap)))
  73. return -1;
  74. #endif
  75. self->capacity = new_cap;
  76. return 0;
  77. }
  78. /*
  79. Create a new textbuffer object.
  80. */
  81. Textbuffer* Textbuffer_new(TokenizerInput* text)
  82. {
  83. Textbuffer* self = malloc(sizeof(Textbuffer));
  84. Unicode maxchar = 0;
  85. #ifdef PEP_393
  86. maxchar = PyUnicode_MAX_CHAR_VALUE(text->object);
  87. #endif
  88. if (!self)
  89. goto fail_nomem;
  90. if (internal_alloc(self, maxchar) < 0)
  91. goto fail_dealloc;
  92. return self;
  93. fail_dealloc:
  94. free(self);
  95. fail_nomem:
  96. PyErr_NoMemory();
  97. return NULL;
  98. }
  99. /*
  100. Deallocate the given textbuffer.
  101. */
  102. void Textbuffer_dealloc(Textbuffer* self)
  103. {
  104. internal_dealloc(self);
  105. free(self);
  106. }
  107. /*
  108. Reset a textbuffer to its initial, empty state.
  109. */
  110. int Textbuffer_reset(Textbuffer* self)
  111. {
  112. Unicode maxchar = 0;
  113. #ifdef PEP_393
  114. maxchar = PyUnicode_MAX_CHAR_VALUE(self->object);
  115. #endif
  116. internal_dealloc(self);
  117. if (internal_alloc(self, maxchar))
  118. return -1;
  119. return 0;
  120. }
  121. /*
  122. Write a Unicode codepoint to the given textbuffer.
  123. */
  124. int Textbuffer_write(Textbuffer* self, Unicode code)
  125. {
  126. if (self->length >= self->capacity) {
  127. if (internal_resize(self, self->capacity * RESIZE_FACTOR) < 0)
  128. return -1;
  129. }
  130. #ifdef PEP_393
  131. PyUnicode_WRITE(self->kind, self->data, self->length++, code);
  132. #else
  133. self->data[self->length++] = code;
  134. #endif
  135. return 0;
  136. }
  137. /*
  138. Read a Unicode codepoint from the given index of the given textbuffer.
  139. This function does not check for bounds.
  140. */
  141. Unicode Textbuffer_read(Textbuffer* self, Py_ssize_t index)
  142. {
  143. #ifdef PEP_393
  144. return PyUnicode_READ(self->kind, self->data, index);
  145. #else
  146. return self->data[index];
  147. #endif
  148. }
  149. /*
  150. Return the contents of the textbuffer as a Python Unicode object.
  151. */
  152. PyObject* Textbuffer_render(Textbuffer* self)
  153. {
  154. #ifdef PEP_393
  155. return PyUnicode_FromKindAndData(self->kind, self->data, self->length);
  156. #else
  157. return PyUnicode_FromUnicode(self->data, self->length);
  158. #endif
  159. }
  160. /*
  161. Concatenate the 'other' textbuffer onto the end of the given textbuffer.
  162. */
  163. int Textbuffer_concat(Textbuffer* self, Textbuffer* other)
  164. {
  165. Py_ssize_t newlen = self->length + other->length;
  166. if (newlen > self->capacity) {
  167. if (internal_resize(self, newlen + CONCAT_EXTRA) < 0)
  168. return -1;
  169. }
  170. #ifdef PEP_393
  171. assert(self->kind == other->kind);
  172. memcpy(((Py_UCS1*) self->data) + self->kind * self->length, other->data,
  173. other->length * other->kind);
  174. #else
  175. memcpy(self->data + self->length, other->data,
  176. other->length * sizeof(Unicode));
  177. #endif
  178. self->length = newlen;
  179. return 0;
  180. }
  181. /*
  182. Reverse the contents of the given textbuffer.
  183. */
  184. void Textbuffer_reverse(Textbuffer* self)
  185. {
  186. Py_ssize_t i, end = self->length - 1;
  187. Unicode tmp;
  188. for (i = 0; i < self->length / 2; i++) {
  189. #ifdef PEP_393
  190. tmp = PyUnicode_READ(self->kind, self->data, i);
  191. PyUnicode_WRITE(self->kind, self->data, i,
  192. PyUnicode_READ(self->kind, self->data, end - i));
  193. PyUnicode_WRITE(self->kind, self->data, end - i, tmp);
  194. #else
  195. tmp = self->data[i];
  196. self->data[i] = self->data[end - i];
  197. self->data[end - i] = tmp;
  198. #endif
  199. }
  200. }