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.
 
 
 
 

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