Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

heh

  • Loading branch information...
commit 2bfd092288abcb98bdc119a5e43f01c12984d449 0 parents
@atsuoishimoto atsuoishimoto authored
Showing with 39,419 additions and 0 deletions.
  1. +171 −0 README.rst
  2. +9,546 −0 _gappedbuf/_gappedbuf.c
  3. +42 −0 _gappedbuf/_gappedbuf.h
  4. +48 −0 _gappedbuf/_gappedbuf.pxd
  5. +486 −0 _gappedbuf/_gappedbuf.pyx
  6. +3 −0  _gappedbuf/_gappedbufre.h
  7. +17 −0 _gappedbuf/rtdef.pxd
  8. +3,996 −0 _gappedbufre/_sre.c
  9. +100 −0 _gappedbufre/sre.h
  10. +86 −0 _gappedbufre/sre_constants.h
  11. +1 −0  build/lib.macosx-10.8-x86_64-3.3/gappedbuf/__init__.py
  12. +360 −0 build/lib.macosx-10.8-x86_64-3.3/gappedbuf/re.py
  13. +521 −0 build/lib.macosx-10.8-x86_64-3.3/gappedbuf/sre_compile.py
  14. +259 −0 build/lib.macosx-10.8-x86_64-3.3/gappedbuf/sre_constants.py
  15. +859 −0 build/lib.macosx-10.8-x86_64-3.3/gappedbuf/sre_parse.py
  16. +45 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/__init__.py
  17. +33 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/command.py
  18. 0  build/lib.macosx-10.8-x86_64-3.3/kaa/commands/__init__.py
  19. +120 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/commands/appcommand.py
  20. +288 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/commands/editorcommand.py
  21. +125 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/commands/filecommand.py
  22. +36 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/context.py
  23. 0  build/lib.macosx-10.8-x86_64-3.3/kaa/cui/__init__.py
  24. +197 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/cui/app.py
  25. +30 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/cui/color.py
  26. +15 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/cui/commandwnd.py
  27. +80 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/cui/dialog.py
  28. +329 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/cui/editor.py
  29. +180 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/cui/frame.py
  30. +299 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/cui/keydef.py
  31. +69 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/cui/main.py
  32. +231 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/cui/splitter.py
  33. +220 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/cui/wnd.py
  34. +165 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/cursor.py
  35. +347 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/document.py
  36. +111 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/fileio.py
  37. 0  build/lib.macosx-10.8-x86_64-3.3/kaa/filetype/__init__.py
  38. +1 −0  build/lib.macosx-10.8-x86_64-3.3/kaa/filetype/default/__init__.py
  39. +48 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/filetype/default/defaultmode.py
  40. +93 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/filetype/default/keybind.py
  41. +218 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/filetype/default/modebase.py
  42. +5 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/filetype/default/theme.py
  43. +8 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/filetype/python/__init__.py
  44. +31 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/filetype/python/pythonmode.py
  45. +327 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/highlight.py
  46. +146 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/keyboard.py
  47. +25 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/keydispatcher.py
  48. +48 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/macro.py
  49. +7 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/options.py
  50. +605 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/screen.py
  51. +2 −0  build/lib.macosx-10.8-x86_64-3.3/kaa/statusbar.py
  52. +38 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/theme.py
  53. +79 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/tools.py
  54. 0  build/lib.macosx-10.8-x86_64-3.3/kaa/ui/__init__.py
  55. +1 −0  build/lib.macosx-10.8-x86_64-3.3/kaa/ui/dialog/__init__.py
  56. +164 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/ui/dialog/dialogmode.py
  57. 0  build/lib.macosx-10.8-x86_64-3.3/kaa/ui/fileopendlg/__init__.py
  58. +443 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/ui/fileopendlg/fileopendlgmode.py
  59. 0  build/lib.macosx-10.8-x86_64-3.3/kaa/ui/framelist/__init__.py
  60. +125 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/ui/framelist/framelistmode.py
  61. 0  build/lib.macosx-10.8-x86_64-3.3/kaa/ui/itemlist/__init__.py
  62. +109 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/ui/itemlist/itemlistmode.py
  63. +1 −0  build/lib.macosx-10.8-x86_64-3.3/kaa/ui/mainmenu/__init__.py
  64. +82 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/ui/mainmenu/mainmenumode.py
  65. 0  build/lib.macosx-10.8-x86_64-3.3/kaa/ui/messagebar/__init__.py
  66. +22 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/ui/messagebar/messagebarmode.py
  67. +1 −0  build/lib.macosx-10.8-x86_64-3.3/kaa/ui/moveseparator/__init__.py
  68. +78 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/ui/moveseparator/moveseparatormode.py
  69. 0  build/lib.macosx-10.8-x86_64-3.3/kaa/ui/msgbox/__init__.py
  70. +107 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/ui/msgbox/msgboxmode.py
  71. 0  build/lib.macosx-10.8-x86_64-3.3/kaa/ui/searchdlg/__init__.py
  72. +9 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/ui/searchdlg/keybind.py
  73. +54 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/ui/searchdlg/searchcommand.py
  74. +355 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/ui/searchdlg/searchdlgmode.py
  75. 0  build/lib.macosx-10.8-x86_64-3.3/kaa/ui/statusbar/__init__.py
  76. +76 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/ui/statusbar/statusbarmode.py
  77. +15 −0 build/lib.macosx-10.8-x86_64-3.3/kaa/utils.py
  78. +1 −0  gappedbuf/__init__.py
  79. +360 −0 gappedbuf/re.py
  80. +521 −0 gappedbuf/sre_compile.py
  81. +259 −0 gappedbuf/sre_constants.py
  82. +859 −0 gappedbuf/sre_parse.py
  83. +45 −0 kaa/__init__.py
  84. +33 −0 kaa/command.py
  85. 0  kaa/commands/__init__.py
  86. +120 −0 kaa/commands/appcommand.py
  87. +288 −0 kaa/commands/editorcommand.py
  88. +125 −0 kaa/commands/filecommand.py
  89. +36 −0 kaa/context.py
  90. 0  kaa/cui/__init__.py
  91. +197 −0 kaa/cui/app.py
  92. +30 −0 kaa/cui/color.py
  93. +15 −0 kaa/cui/commandwnd.py
  94. +80 −0 kaa/cui/dialog.py
  95. +329 −0 kaa/cui/editor.py
  96. +180 −0 kaa/cui/frame.py
  97. +297 −0 kaa/cui/keydef.py
  98. +69 −0 kaa/cui/main.py
  99. +231 −0 kaa/cui/splitter.py
  100. +220 −0 kaa/cui/wnd.py
  101. +165 −0 kaa/cursor.py
  102. +347 −0 kaa/document.py
  103. +111 −0 kaa/fileio.py
  104. 0  kaa/filetype/__init__.py
  105. +1 −0  kaa/filetype/default/__init__.py
  106. +48 −0 kaa/filetype/default/defaultmode.py
  107. +94 −0 kaa/filetype/default/keybind.py
  108. +218 −0 kaa/filetype/default/modebase.py
  109. +5 −0 kaa/filetype/default/theme.py
  110. +8 −0 kaa/filetype/python/__init__.py
  111. +31 −0 kaa/filetype/python/pythonmode.py
  112. +327 −0 kaa/highlight.py
  113. +146 −0 kaa/keyboard.py
  114. +25 −0 kaa/keydispatcher.py
  115. +48 −0 kaa/macro.py
  116. +7 −0 kaa/options.py
  117. +605 −0 kaa/screen.py
  118. +2 −0  kaa/statusbar.py
  119. +38 −0 kaa/theme.py
  120. +79 −0 kaa/tools.py
  121. 0  kaa/ui/__init__.py
  122. +1 −0  kaa/ui/dialog/__init__.py
  123. +164 −0 kaa/ui/dialog/dialogmode.py
  124. 0  kaa/ui/fileopendlg/__init__.py
  125. +443 −0 kaa/ui/fileopendlg/fileopendlgmode.py
  126. 0  kaa/ui/framelist/__init__.py
  127. +125 −0 kaa/ui/framelist/framelistmode.py
  128. 0  kaa/ui/itemlist/__init__.py
  129. +109 −0 kaa/ui/itemlist/itemlistmode.py
  130. +1 −0  kaa/ui/mainmenu/__init__.py
  131. +82 −0 kaa/ui/mainmenu/mainmenumode.py
  132. 0  kaa/ui/messagebar/__init__.py
  133. +22 −0 kaa/ui/messagebar/messagebarmode.py
  134. +1 −0  kaa/ui/moveseparator/__init__.py
  135. +78 −0 kaa/ui/moveseparator/moveseparatormode.py
  136. 0  kaa/ui/msgbox/__init__.py
  137. +107 −0 kaa/ui/msgbox/msgboxmode.py
  138. 0  kaa/ui/searchdlg/__init__.py
  139. +9 −0 kaa/ui/searchdlg/keybind.py
  140. +54 −0 kaa/ui/searchdlg/searchcommand.py
  141. +355 −0 kaa/ui/searchdlg/searchdlgmode.py
  142. 0  kaa/ui/statusbar/__init__.py
  143. +76 −0 kaa/ui/statusbar/statusbarmode.py
  144. +15 −0 kaa/utils.py
  145. +3,967 −0 save-re/re_3.3/_sre.c
  146. +360 −0 save-re/re_3.3/re.py
  147. +100 −0 save-re/re_3.3/sre.h
  148. +518 −0 save-re/re_3.3/sre_compile.py
  149. +86 −0 save-re/re_3.3/sre_constants.h
  150. +259 −0 save-re/re_3.3/sre_constants.py
  151. +859 −0 save-re/re_3.3/sre_parse.py
  152. +44 −0 setup.py
  153. +79 −0 test/kaa_testutils.py
  154. +40 −0 test/test_cuiapp.py
  155. +132 −0 test/test_cursor.py
  156. +79 −0 test/test_dialog.py
  157. +98 −0 test/test_editorcommands.py
  158. +23 −0 test/test_framelist.py
  159. +297 −0 test/test_highlight.py
  160. +128 −0 test/test_keyboard.py
  161. +60 −0 test/test_macro.py
  162. +22 −0 test/test_mainmenu.py
  163. +76 −0 test/test_modebase.py
  164. +17 −0 test/test_msgbox.py
  165. +441 −0 test/test_screen.py
  166. +85 −0 test/test_searchdlgmode.py
  167. +18 −0 test/test_statusbarmode.py
  168. +292 −0 test/test_textdocument.py
  169. +319 −0 test_gappedbuf/test_buf.py
  170. +40 −0 test_gappedbuf/test_bufre.py
171 README.rst
@@ -0,0 +1,171 @@
+============================
+Kaa - console text editor
+============================
+
+Kaa is a small and easy text editor for console environment.
+
+.. DANGER::
+ Kaa is still in the very early stage of development. Don't use kaa for other purpose than evaluation.
+
+.. contents::
+
+Requirements
+============
+
+* Python 3.3 or later
+
+* Headers and librares for ncurses. Consult documentation of your platform for detail. With Debian/Ubuntu, you can install ncurses library with ::
+
+ $ sudo apt-get install libncurses-dev
+
+* (optional) Cython
+
+Installation
+============
+
+1. Get source from ::
+
+ $ git clone
+
+2. Install kaa ::
+
+ $ cd kaa
+ $ python setup.py install
+
+Command line options
+====================
+
+To start kaa, type ::
+
+ $ kaa [FILE]...
+
+Usage
+=====
+
+Using kaa is intuitive. Typing alphabet keys will update file as you expected. Functional keys like arrow or delete keys also works.
+
+Using menu
+-----------
+
+To display menu, type F1 key. Each items in the menu has one underlined character. Typing the underlined character extecutes the menu item.
+
+To hide menu, type escape key.
+
+File menu
+++++++++++
+
+Open
+ Open existing file.
+
+Save
+ Save current file.
+
+Save As
+ Save current file as new file.
+
+Close
+ Close current frame.
+
+Quit
+ Terminate kaa.
+
+
+Edit menu
++++++++++
+
+Cut
+ Cut selected text.
+
+Copy
+ Copy selected text.
+
+Paste
+ Paste from clipboard.
+
+Undo
+ Undo last modification.
+
+Redo
+ Redo last undo.
+
+
+Macro menu
+++++++++++
+
+Start record
+ Start macro recording.
+
+End record
+ End macro recording.
+
+Run macro
+ Run last macro.
+
+
+Window menu
++++++++++++
+
+Frame list
+ Show list of frame windows. Use left/right arrow key to change active frame.
+
+Split vert
+ Split current window vertically.
+
+Split horz
+ Split current window horizontally.
+
+Move separator
+ Move window separator. Use left/right arrow key to move separator.
+
+Next window
+ Activate next window.
+
+Join window
+ Join splitted window.
+
+Switch file
+ Switch content of active window.
+
+
+Key bindings
+------------
+
+Arrow keys
+ Move cursor.
+
+Shift+arrow keys
+ Select text.
+
+Control+left/right arrow keys
+ Move cursor to next/prev word boundary.
+
+Backspace
+ Delete the character to the left.
+
+Delete
+ Delete the character at the cursor.
+
+Control+z
+ Undo last change.
+
+Control+r
+ Redo last undoi.
+
+F6
+ Toggle macro recording on/off.
+
+F5
+ Run macro.
+
+Control+S
+ Search text.
+
+Alt+S
+ Replace text.
+
+Control+b|Control+f|Control+p|Control+n
+ Move cursor to left|right|up|down.
+
+Alt+b|Alt+f
+ Move cursor to next/prev word boundary.
+
9,546 _gappedbuf/_gappedbuf.c
9,546 additions, 0 deletions not shown
42 _gappedbuf/_gappedbuf.h
@@ -0,0 +1,42 @@
+#ifndef __PYX_HAVE___gappedbuf
+#define __PYX_HAVE___gappedbuf
+
+struct GappedBufferObj;
+
+/* "_gappedbuf.pxd":2
+ *
+ * cdef public class GappedBuffer [object GappedBufferObj, type GappedBufferType]: # <<<<<<<<<<<<<<
+ * cdef public Py_ssize_t bufsize
+ * cdef public Py_ssize_t numelems
+ */
+struct GappedBufferObj {
+ PyObject_HEAD
+ struct __pyx_vtabstruct_10_gappedbuf_GappedBuffer *__pyx_vtab;
+ Py_ssize_t bufsize;
+ Py_ssize_t numelems;
+ Py_ssize_t gap;
+ Py_ssize_t gapsize;
+ Py_UCS4 *buf;
+};
+
+#ifndef __PYX_HAVE_API___gappedbuf
+
+#ifndef __PYX_EXTERN_C
+ #ifdef __cplusplus
+ #define __PYX_EXTERN_C extern "C"
+ #else
+ #define __PYX_EXTERN_C extern
+ #endif
+#endif
+
+__PYX_EXTERN_C DL_IMPORT(PyTypeObject) GappedBufferType;
+
+#endif /* !__PYX_HAVE_API___gappedbuf */
+
+#if PY_MAJOR_VERSION < 3
+PyMODINIT_FUNC init_gappedbuf(void);
+#else
+PyMODINIT_FUNC PyInit__gappedbuf(void);
+#endif
+
+#endif /* !__PYX_HAVE___gappedbuf */
48 _gappedbuf/_gappedbuf.pxd
@@ -0,0 +1,48 @@
+
+cdef public class GappedBuffer [object GappedBufferObj, type GappedBufferType]:
+ cdef public Py_ssize_t bufsize
+ cdef public Py_ssize_t numelems
+ cdef public Py_ssize_t gap
+ cdef public Py_ssize_t gapsize
+ cdef Py_UCS4 *buf
+
+# cdef inline _conv_slice(self, Py_ssize_t pos) nogil:
+# if pos < 0:
+#
+# return self.numelems
+
+ cdef void _resize_buf(self, Py_ssize_t size)
+ cdef void _expand_gap(self, Py_ssize_t size)
+ cdef void _shrink_gap(self)
+ cdef void _move_gap(self, Py_ssize_t index)
+
+ cdef Py_UCS4 _get_max(self, Py_ssize_t begin, Py_ssize_t end) nogil
+ cdef void _copy_chars1(self, void *dest, Py_ssize_t begin,
+ Py_ssize_t end) nogil
+ cdef void _copy_chars2(self, void *dest, Py_ssize_t begin,
+ Py_ssize_t end) nogil
+ cdef void _copy_chars4(self, void *dest, Py_ssize_t begin,
+ Py_ssize_t end) nogil
+
+ cdef int _get_slice(self, object obj,
+ Py_ssize_t *f, Py_ssize_t *t) except *
+
+ cdef _insert(self, Py_ssize_t index, unicode s)
+ cpdef insert(self, Py_ssize_t index, unicode s)
+ cpdef append(self, unicode s)
+ cdef _delete(self, Py_ssize_t begin, Py_ssize_t end)
+ cpdef delete(self, Py_ssize_t begin, Py_ssize_t end)
+ cpdef replace(self, Py_ssize_t begin, Py_ssize_t end, unicode s)
+ cpdef get(self, Py_ssize_t begin, Py_ssize_t end)
+
+ cdef int _get_slice(self, object obj,
+ Py_ssize_t *f, Py_ssize_t *t) except *
+ cpdef getints(self, Py_ssize_t begin, Py_ssize_t end)
+ cdef _insertints(self, Py_ssize_t index, object s)
+ cpdef insertints(self, Py_ssize_t index, object s)
+ cpdef appendints(self, object s)
+ cpdef replaceints(self, Py_ssize_t begin, Py_ssize_t end, object s)
+ cpdef setints(self, Py_ssize_t begin, Py_ssize_t end, unsigned long v)
+
+
+
486 _gappedbuf/_gappedbuf.pyx
@@ -0,0 +1,486 @@
+from libc.stdlib cimport malloc, free, realloc
+from libc.string cimport memmove, memcpy
+from cpython cimport unicode, mem, ref
+from cpython.list cimport PyList_New, PyList_SET_ITEM
+from cpython.long cimport PyLong_FromUnsignedLong
+
+cimport rtdef
+
+cdef extern from "_gappedbufre.h":
+ object PyInit__gappedbufre()
+
+
+
+def init_gappedbufre():
+ import sys
+ sys.modules['_gappedbufre'] = PyInit__gappedbufre()
+
+cdef class GappedBuffer:
+ def __cinit__(self):
+ DEF BUFSIZE = 4096
+
+ self.buf = <Py_UCS4*>malloc(BUFSIZE * sizeof(Py_UCS4))
+ if not self.buf:
+ raise MemoryError()
+
+ self.bufsize = BUFSIZE
+ self.numelems = 0
+ self.gap = 0
+ self.gapsize = BUFSIZE
+
+ def __dealloc__(self):
+ if self.buf:
+ free(self.buf)
+ self.buf = NULL
+
+ cdef void _resize_buf(self, Py_ssize_t size):
+ cdef Py_UCS4 *buf = <Py_UCS4*>realloc(
+ self.buf, size * sizeof(Py_UCS4))
+
+ if not buf:
+ raise MemoryError()
+ self.buf = buf
+ self.bufsize = size
+
+ cdef void _expand_gap(self, Py_ssize_t size):
+ if size <= self.gapsize:
+ return
+ self._resize_buf(self.numelems+size)
+
+ if self.gap < self.numelems:
+ # resize gap
+ memmove(self.buf+self.gap+size, self.buf+self.gap+self.gapsize,
+ (self.numelems-self.gap) * sizeof(Py_UCS4))
+
+ self.gapsize = size
+
+ cdef void _shrink_gap(self):
+ DEF MAX_GAPSIZE = 4096
+ if self.numelems + MAX_GAPSIZE > self.bufsize:
+ return
+ memmove(self.buf+self.gap+MAX_GAPSIZE,
+ self.buf+self.gap+self.gapsize,
+ (self.numelems-self.gap) * sizeof(Py_UCS4))
+
+ self.gapsize = MAX_GAPSIZE
+ self._resize_buf(self.numelems+MAX_GAPSIZE)
+
+ cdef void _move_gap(self, Py_ssize_t index):
+ assert index <= self.bufsize
+ if self.gap == index:
+ return
+ if self.gapsize:
+ if index < self.gap:
+ memmove(self.buf+index+self.gapsize, self.buf+index,
+ (self.gap - index)*sizeof(Py_UCS4))
+ else:
+ memmove(self.buf+self.gap, self.buf+self.gap+self.gapsize,
+ (index - self.gap)*sizeof(Py_UCS4))
+ self.gap = index
+
+ cdef _insert(self, Py_ssize_t index, unicode s):
+ cdef Py_ssize_t size = len(s)
+ if not (0 <= index <= self.numelems):
+ raise ValueError('Invalid index value')
+
+ if size:
+ self._move_gap(index)
+ if self.gapsize < size:
+ self._expand_gap(max(1024, size*2))
+ rtdef.PyUnicode_AsUCS4(s, self.buf+index, size, False)
+
+ self.numelems += size
+ self.gap += size
+ self.gapsize -= size
+
+ cpdef insert(self, Py_ssize_t index, unicode s):
+ self._insert(index, s)
+
+ cpdef append(self, unicode s):
+ self.insert(self.numelems, s)
+
+ cdef _delete(self, Py_ssize_t begin, Py_ssize_t end):
+ if not (0 <= begin <= end <= self.numelems):
+ raise ValueError('Invalid range')
+
+ cdef Py_ssize_t size = end-begin
+ if not size:
+ return
+
+ self._move_gap(begin)
+ self.gapsize += size
+ self.numelems -= size
+ self._shrink_gap()
+
+ cpdef delete(self, Py_ssize_t begin, Py_ssize_t end):
+ self._delete(begin, end)
+
+ cpdef replace(self, Py_ssize_t begin, Py_ssize_t end, unicode s):
+ self._delete(begin, end)
+ self._insert(begin, s)
+
+ cdef Py_UCS4 _get_max(self, Py_ssize_t begin, Py_ssize_t end) nogil:
+ cdef Py_UCS4 maxchar = 0
+ cdef Py_ssize_t b, e
+
+ e = min(end, self.gap)
+
+ cdef Py_ssize_t p
+ for p from begin <= p < e:
+ maxchar = max(maxchar, self.buf[p])
+ b = max(begin, self.gap)
+ for p from b <= p < end:
+ maxchar = max(maxchar, self.buf[p+self.gapsize])
+ return maxchar
+
+ cdef void _copy_chars1(self, void *dest, Py_ssize_t begin,
+ Py_ssize_t end) nogil:
+ cdef rtdef.Py_UCS1 *buf = <rtdef.Py_UCS1 *>dest
+ cdef Py_ssize_t b, e
+
+ # copy before gap
+ e = min(end, self.gap)
+ cdef Py_ssize_t p
+ for p from begin <= p < e:
+ buf[0] = self.buf[p]
+ buf += 1
+
+ # copy after gap
+ b = max(begin, self.gap) + self.gapsize
+ for p from b <= p < end+self.gapsize:
+ buf[0] = self.buf[p]
+ buf += 1
+
+ cdef void _copy_chars2(self, void *dest, Py_ssize_t begin,
+ Py_ssize_t end) nogil:
+ cdef rtdef.Py_UCS2 *buf = <rtdef.Py_UCS2 *>dest
+ cdef Py_ssize_t b, e
+
+ # copy before gap
+ e = min(end, self.gap)
+ cdef Py_ssize_t p
+ for p from begin <= p < e:
+ buf[0] = self.buf[p]
+ buf += 1
+
+ # copy after gap
+ b = max(begin, self.gap) + self.gapsize
+ for p from b <= p < end+self.gapsize:
+ buf[0] = self.buf[p]
+ buf += 1
+
+ cdef void _copy_chars4(self, void *dest, Py_ssize_t begin,
+ Py_ssize_t end) nogil:
+ cdef Py_ssize_t n = end - begin
+ cdef Py_ssize_t former
+
+ if self.gap < begin:
+ memcpy(dest, self.buf+begin+self.gapsize, n*sizeof(Py_UCS4))
+ elif end <= self.gap:
+ memcpy(dest, self.buf+begin, n*sizeof(Py_UCS4))
+ else:
+ former = self.gap - begin
+ memcpy(dest, self.buf+begin, former*sizeof(Py_UCS4))
+ memcpy(dest+former*sizeof(Py_UCS4), self.buf+self.gap+self.gapsize,
+ (n-former)*sizeof(Py_UCS4))
+
+ cpdef get(self, Py_ssize_t begin, Py_ssize_t end):
+ if not (0 <= begin <= end <= self.numelems):
+ raise IndexError('index out of range')
+
+ cdef Py_ssize_t n = end - begin
+ cdef Py_UCS4 maxchar = self._get_max(begin, end)
+ cdef unicode ret = rtdef.PyUnicode_New(n, maxchar)
+ cdef void *buf = rtdef.PyUnicode_DATA(ret)
+ cdef nbytes = rtdef.PyUnicode_KIND(ret)
+
+ if nbytes == 1:
+ self._copy_chars1(buf, begin, end)
+ elif nbytes == 2:
+ self._copy_chars2(buf, begin, end)
+ elif nbytes == 4:
+ self._copy_chars4(buf, begin, end)
+ else:
+ raise RuntimeError()
+
+ return ret
+
+ def __len__(self):
+ return self.numelems
+
+ cdef int _get_slice(self, object obj,
+ Py_ssize_t *f, Py_ssize_t *t) except *:
+ cdef Py_ssize_t step, slicelength
+
+ if rtdef.PySlice_Check(obj):
+ rtdef.PySlice_GetIndicesEx(obj, self.numelems, f, t,
+ &step, &slicelength)
+ if step != 1:
+ raise ValueError('step is not supported')
+ return 1
+ else:
+ f[0] = obj
+ if f[0] < 0:
+ f[0] += self.numelems
+ if not (0 <= f[0] < self.numelems):
+ raise IndexError('index out of range')
+ return 0
+
+ def __getitem__(self, object item):
+ cdef Py_ssize_t start, stop
+
+ if self._get_slice(item, &start, &stop):
+ return self.get(start, stop)
+ else:
+ return self.buf[start+(0 if start < self.gap else self.gapsize)]
+
+ def __setitem__(self, object item, unicode value):
+ cdef Py_ssize_t start, stop
+
+ if self._get_slice(item, &start, &stop):
+ self.replace(start, stop, value)
+ else:
+ if len(value) != 1:
+ raise ValueError('Invalid string length.')
+
+ start = start+(0 if start < self.gap else self.gapsize)
+ self.buf[start] = <Py_UCS4>value
+
+ def __delitem__(self, object item):
+ cdef Py_ssize_t start, stop
+
+ if self._get_slice(item, &start, &stop):
+ self.delete(start, stop)
+ else:
+ self.delete(start, start+1)
+
+ def findchr(self, unicode c, Py_ssize_t begin, Py_ssize_t end):
+ cdef Py_UCS4 *chars
+ cdef Py_ssize_t size, b, e, p
+
+ if not (0 <= begin <= end <= self.numelems):
+ raise ValueError('Invalid range')
+
+ chars = rtdef.PyUnicode_AsUCS4Copy(c)
+ try:
+ size = len(c)
+ # search before gap
+ e = min(end, self.gap)
+ for p from begin <= p < e:
+ for q from 0 <= q < size:
+ if self.buf[p] == chars[q]:
+ return p
+
+ # search after gap
+ b = max(begin, self.gap) + self.gapsize
+ for p from b <= p < end+self.gapsize:
+ for q from 0 <= q < size:
+ if self.buf[p] == chars[q]:
+ return p-self.gapsize
+ finally:
+ mem.PyMem_Free(chars)
+
+ return -1
+
+ def rfindchr(self, unicode c, Py_ssize_t begin, Py_ssize_t end):
+ cdef Py_UCS4 *chars
+ cdef Py_ssize_t size, b, e, p
+
+ if not (0 <= begin <= end <= self.numelems):
+ raise ValueError('Invalid range')
+
+ chars = rtdef.PyUnicode_AsUCS4Copy(c)
+ try:
+ size = len(c)
+ # search after gap
+ b = max(begin, self.gap) + self.gapsize
+ for p from (end+self.gapsize) > p >= b:
+ for q from 0 <= q < size:
+ if self.buf[p] == chars[q]:
+ return p-self.gapsize
+
+ # search before gap
+ e = min(end, self.gap)
+ for p from e > p >= begin:
+ for q from 0 <= q < size:
+ if self.buf[p] == chars[q]:
+ return p
+ finally:
+ mem.PyMem_Free(chars)
+
+ return -1
+
+ cpdef getints(self, Py_ssize_t begin, Py_ssize_t end):
+
+ cdef Py_ssize_t b, e, p, n
+ cdef list ret
+ cdef object v
+
+ if not (0 <= begin <= end <= self.numelems):
+ raise ValueError('Invalid range')
+
+ ret = PyList_New(end-begin)
+ n = 0
+ # read before gap
+ e = min(end, self.gap)
+ for p from begin <= p < e:
+ v = <unsigned long>self.buf[p]
+ PyList_SET_ITEM(ret, n, v)
+ ref.Py_INCREF(v)
+ n += 1
+
+ # read after gap
+ b = max(begin, self.gap) + self.gapsize
+ for p from b <= p < end+self.gapsize:
+ v = <unsigned long>self.buf[p]
+ PyList_SET_ITEM(ret, n, v)
+ ref.Py_INCREF(v)
+ n += 1
+
+ return ret
+
+ cdef _insertints(self, Py_ssize_t index, object s):
+
+ cdef Py_ssize_t size = len(s)
+ if not (0 <= index <= self.numelems):
+ raise ValueError('Invalid index value')
+
+ if size:
+ self._move_gap(index)
+ if self.gapsize < size:
+ self._expand_gap(max(1024, size*2))
+
+ for value in s:
+ self.buf[self.gap] = value
+ self.gap += 1
+
+ self.numelems += size
+ self.gapsize -= size
+
+ cpdef insertints(self, Py_ssize_t index, object s):
+
+ self._insertints(index, s)
+
+ cpdef appendints(self, object s):
+ self.insertints(self.numelems, s)
+
+ cpdef replaceints(self, Py_ssize_t begin, Py_ssize_t end, object s):
+ self._delete(begin, end)
+ self._insertints(begin, s)
+
+ cpdef setints(self, Py_ssize_t begin, Py_ssize_t end, unsigned long v):
+ cdef Py_ssize_t b, e, p
+
+ if not (0 <= begin <= end <= self.numelems):
+ raise ValueError('Invalid range')
+
+ n = 0
+ # replace before gap
+ e = min(end, self.gap)
+ for p from begin <= p < e:
+ self.buf[p] = v
+
+ # replace after gap
+ b = max(begin, self.gap) + self.gapsize
+ for p from b <= p < end+self.gapsize:
+ self.buf[p] = v
+
+ def findint(self, object nums, Py_ssize_t begin, Py_ssize_t end,
+ int comp_ne):
+ cdef unsigned long *longs
+ cdef Py_ssize_t size, b, e, p
+
+ if not (0 <= begin <= end <= self.numelems):
+ raise ValueError('Invalid range')
+
+ size = len(nums)
+ longs = <unsigned long *>mem.PyMem_Malloc(
+ size * sizeof(unsigned long))
+ if not longs:
+ raise MemoryError()
+
+ try:
+ for i from 0 <= i < size:
+ longs[i] = nums[i]
+
+ # search before gap
+ e = min(end, self.gap)
+ for p from begin <= p < e:
+ if not comp_ne:
+ for q from 0 <= q < size:
+ if self.buf[p] == longs[q]:
+ return p
+ else:
+ for q from 0 <= q < size:
+ if self.buf[p] == longs[q]:
+ break
+ else:
+ return p
+
+ # search after gap
+ b = max(begin, self.gap) + self.gapsize
+ for p from b <= p < end+self.gapsize:
+ if not comp_ne:
+ for q from 0 <= q < size:
+ if self.buf[p] == longs[q]:
+ return p-self.gapsize
+ else:
+ for q from 0 <= q < size:
+ if self.buf[p] == longs[q]:
+ break
+ else:
+ return p-self.gapsize
+ finally:
+ mem.PyMem_Free(longs)
+
+ return -1
+
+ def rfindint(self, object nums, Py_ssize_t begin, Py_ssize_t end,
+ int comp_ne):
+
+ cdef unsigned long *longs
+ cdef Py_ssize_t size, b, e, p
+
+ if not (0 <= begin <= end <= self.numelems):
+ raise ValueError('Invalid range')
+
+ size = len(nums)
+ longs = <unsigned long *>mem.PyMem_Malloc(
+ size * sizeof(unsigned long))
+ if not longs:
+ raise MemoryError()
+ try:
+ for i from 0 <= i < size:
+ longs[i] = nums[i]
+
+ # search after gap
+ b = max(begin, self.gap) + self.gapsize
+ for p from (end+self.gapsize) > p >= b:
+ if not comp_ne:
+ for q from 0 <= q < size:
+ if self.buf[p] == longs[q]:
+ return p-self.gapsize
+ else:
+ for q from 0 <= q < size:
+ if self.buf[p] == longs[q]:
+ break
+ else:
+ return p-self.gapsize
+
+ # search before gap
+ e = min(end, self.gap)
+ for p from e > p >= begin:
+ if not comp_ne:
+ for q from 0 <= q < size:
+ if self.buf[p] == longs[q]:
+ return p
+ else:
+ for q from 0 <= q < size:
+ if self.buf[p] == longs[q]:
+ break
+ else:
+ return p
+ finally:
+ mem.PyMem_Free(longs)
+
+ return -1
3  _gappedbuf/_gappedbufre.h
@@ -0,0 +1,3 @@
+
+
+PyObject *PyInit__gappedbufre(void);
17 _gappedbuf/rtdef.pxd
@@ -0,0 +1,17 @@
+
+cdef extern from "Python.h":
+ ctypedef unsigned char Py_UCS1
+ ctypedef unsigned short Py_UCS2
+
+ Py_UCS4* PyUnicode_AsUCS4Copy(object u) except NULL
+ Py_UCS4* PyUnicode_AsUCS4(object u, Py_UCS4 *buffer,
+ Py_ssize_t buflen, int copy_null) except NULL
+ object PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar)
+ void* PyUnicode_DATA(object o) except NULL
+ int PyUnicode_KIND(object o) except 0
+
+ int PySlice_GetIndicesEx(object slice, Py_ssize_t length,
+ Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
+ Py_ssize_t *slicelength) except -1
+ int PySlice_Check(object)
+
3,996 _gappedbufre/_sre.c
3,996 additions, 0 deletions not shown
100 _gappedbufre/sre.h
@@ -0,0 +1,100 @@
+/*
+ * Secret Labs' Regular Expression Engine
+ *
+ * regular expression matching engine
+ *
+ * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
+ *
+ * See the _sre.c file for information on usage and redistribution.
+ */
+
+#ifndef SRE_INCLUDED
+#define SRE_INCLUDED
+
+#include "sre_constants.h"
+
+/* size of a code word (must be unsigned short or larger, and
+ large enough to hold a UCS4 character) */
+#define SRE_CODE Py_UCS4
+#if SIZEOF_SIZE_T > 4
+# define SRE_MAXREPEAT (~(SRE_CODE)0)
+#else
+# define SRE_MAXREPEAT ((SRE_CODE)PY_SSIZE_T_MAX + 1u)
+#endif
+
+typedef struct {
+ PyObject_VAR_HEAD
+ Py_ssize_t groups; /* must be first! */
+ PyObject* groupindex;
+ PyObject* indexgroup;
+ /* compatibility */
+ PyObject* pattern; /* pattern source (or None) */
+ int flags; /* flags used when compiling pattern source */
+ PyObject *weakreflist; /* List of weak references */
+ int logical_charsize; /* pattern charsize (or -1) */
+ int charsize;
+ Py_buffer view;
+ /* pattern code */
+ Py_ssize_t codesize;
+ SRE_CODE code[1];
+} PatternObject;
+
+#define PatternObject_GetCode(o) (((PatternObject*)(o))->code)
+
+typedef struct {
+ PyObject_VAR_HEAD
+ struct GappedBufferObj* string; /* link to the target string (must be first) */
+ PyObject* regs; /* cached list of matching spans */
+ PatternObject* pattern; /* link to the regex (pattern) object */
+ Py_ssize_t pos, endpos; /* current target slice */
+ Py_ssize_t lastindex; /* last index marker seen by the engine (-1 if none) */
+ Py_ssize_t groups; /* number of groups (start/end marks) */
+ Py_ssize_t mark[1];
+} MatchObject;
+
+typedef unsigned int (*SRE_TOLOWER_HOOK)(unsigned int ch);
+
+/* FIXME: <fl> shouldn't be a constant, really... */
+#define SRE_MARK_SIZE 200
+
+typedef struct SRE_REPEAT_T {
+ Py_ssize_t count;
+ SRE_CODE* pattern; /* points to REPEAT operator arguments */
+ void* last_ptr; /* helper to check for infinite loops */
+ struct SRE_REPEAT_T *prev; /* points to previous repeat context */
+} SRE_REPEAT;
+
+typedef struct {
+ /* string pointers */
+ void* ptr; /* current position (also end of current slice) */
+ void* beginning; /* start of original string */
+ void* start; /* start of current slice */
+ void* end; /* end of original string */
+ /* attributes for the match object */
+ struct GappedBufferObj* string;
+ Py_ssize_t pos, endpos;
+ /* character size */
+ int logical_charsize; /* kind of thing: 1 - bytes, 2/4 - unicode */
+ int charsize;
+ /* registers */
+ Py_ssize_t lastindex;
+ Py_ssize_t lastmark;
+ void* mark[SRE_MARK_SIZE];
+ /* dynamically allocated stuff */
+ char* data_stack;
+ size_t data_stack_size;
+ size_t data_stack_base;
+ Py_buffer buffer;
+ /* current repeat context */
+ SRE_REPEAT *repeat;
+ /* hooks */
+ SRE_TOLOWER_HOOK lower;
+} SRE_STATE;
+
+typedef struct {
+ PyObject_HEAD
+ PyObject* pattern;
+ SRE_STATE state;
+} ScannerObject;
+
+#endif
86 _gappedbufre/sre_constants.h
@@ -0,0 +1,86 @@
+/*
+ * Secret Labs' Regular Expression Engine
+ *
+ * regular expression matching engine
+ *
+ * NOTE: This file is generated by sre_constants.py. If you need
+ * to change anything in here, edit sre_constants.py and run it.
+ *
+ * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
+ *
+ * See the _sre.c file for information on usage and redistribution.
+ */
+
+#define SRE_MAGIC 20031017
+#define SRE_OP_FAILURE 0
+#define SRE_OP_SUCCESS 1
+#define SRE_OP_ANY 2
+#define SRE_OP_ANY_ALL 3
+#define SRE_OP_ASSERT 4
+#define SRE_OP_ASSERT_NOT 5
+#define SRE_OP_AT 6
+#define SRE_OP_BRANCH 7
+#define SRE_OP_CALL 8
+#define SRE_OP_CATEGORY 9
+#define SRE_OP_CHARSET 10
+#define SRE_OP_BIGCHARSET 11
+#define SRE_OP_GROUPREF 12
+#define SRE_OP_GROUPREF_EXISTS 13
+#define SRE_OP_GROUPREF_IGNORE 14
+#define SRE_OP_IN 15
+#define SRE_OP_IN_IGNORE 16
+#define SRE_OP_INFO 17
+#define SRE_OP_JUMP 18
+#define SRE_OP_LITERAL 19
+#define SRE_OP_LITERAL_IGNORE 20
+#define SRE_OP_MARK 21
+#define SRE_OP_MAX_UNTIL 22
+#define SRE_OP_MIN_UNTIL 23
+#define SRE_OP_NOT_LITERAL 24
+#define SRE_OP_NOT_LITERAL_IGNORE 25
+#define SRE_OP_NEGATE 26
+#define SRE_OP_RANGE 27
+#define SRE_OP_REPEAT 28
+#define SRE_OP_REPEAT_ONE 29
+#define SRE_OP_SUBPATTERN 30
+#define SRE_OP_MIN_REPEAT_ONE 31
+#define SRE_AT_BEGINNING 0
+#define SRE_AT_BEGINNING_LINE 1
+#define SRE_AT_BEGINNING_STRING 2
+#define SRE_AT_BOUNDARY 3
+#define SRE_AT_NON_BOUNDARY 4
+#define SRE_AT_END 5
+#define SRE_AT_END_LINE 6
+#define SRE_AT_END_STRING 7
+#define SRE_AT_LOC_BOUNDARY 8
+#define SRE_AT_LOC_NON_BOUNDARY 9
+#define SRE_AT_UNI_BOUNDARY 10
+#define SRE_AT_UNI_NON_BOUNDARY 11
+#define SRE_CATEGORY_DIGIT 0
+#define SRE_CATEGORY_NOT_DIGIT 1
+#define SRE_CATEGORY_SPACE 2
+#define SRE_CATEGORY_NOT_SPACE 3
+#define SRE_CATEGORY_WORD 4
+#define SRE_CATEGORY_NOT_WORD 5
+#define SRE_CATEGORY_LINEBREAK 6
+#define SRE_CATEGORY_NOT_LINEBREAK 7
+#define SRE_CATEGORY_LOC_WORD 8
+#define SRE_CATEGORY_LOC_NOT_WORD 9
+#define SRE_CATEGORY_UNI_DIGIT 10
+#define SRE_CATEGORY_UNI_NOT_DIGIT 11
+#define SRE_CATEGORY_UNI_SPACE 12
+#define SRE_CATEGORY_UNI_NOT_SPACE 13
+#define SRE_CATEGORY_UNI_WORD 14
+#define SRE_CATEGORY_UNI_NOT_WORD 15
+#define SRE_CATEGORY_UNI_LINEBREAK 16
+#define SRE_CATEGORY_UNI_NOT_LINEBREAK 17
+#define SRE_FLAG_TEMPLATE 1
+#define SRE_FLAG_IGNORECASE 2
+#define SRE_FLAG_LOCALE 4
+#define SRE_FLAG_MULTILINE 8
+#define SRE_FLAG_DOTALL 16
+#define SRE_FLAG_UNICODE 32
+#define SRE_FLAG_VERBOSE 64
+#define SRE_INFO_PREFIX 1
+#define SRE_INFO_LITERAL 2
+#define SRE_INFO_CHARSET 4
1  build/lib.macosx-10.8-x86_64-3.3/gappedbuf/__init__.py
@@ -0,0 +1 @@
+from _gappedbuf import *
360 build/lib.macosx-10.8-x86_64-3.3/gappedbuf/re.py
@@ -0,0 +1,360 @@
+#
+# Secret Labs' Regular Expression Engine
+#
+# re-compatible interface for the sre matching engine
+#
+# Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved.
+#
+# This version of the SRE library can be redistributed under CNRI's
+# Python 1.6 license. For any other use, please contact Secret Labs
+# AB (info@pythonware.com).
+#
+# Portions of this engine have been developed in cooperation with
+# CNRI. Hewlett-Packard provided funding for 1.6 integration and
+# other compatibility work.
+#
+
+r"""Support for regular expressions (RE).
+
+This module provides regular expression matching operations similar to
+those found in Perl. It supports both 8-bit and Unicode strings; both
+the pattern and the strings being processed can contain null bytes and
+characters outside the US ASCII range.
+
+Regular expressions can contain both special and ordinary characters.
+Most ordinary characters, like "A", "a", or "0", are the simplest
+regular expressions; they simply match themselves. You can
+concatenate ordinary characters, so last matches the string 'last'.
+
+The special characters are:
+ "." Matches any character except a newline.
+ "^" Matches the start of the string.
+ "$" Matches the end of the string or just before the newline at
+ the end of the string.
+ "*" Matches 0 or more (greedy) repetitions of the preceding RE.
+ Greedy means that it will match as many repetitions as possible.
+ "+" Matches 1 or more (greedy) repetitions of the preceding RE.
+ "?" Matches 0 or 1 (greedy) of the preceding RE.
+ *?,+?,?? Non-greedy versions of the previous three special characters.
+ {m,n} Matches from m to n repetitions of the preceding RE.
+ {m,n}? Non-greedy version of the above.
+ "\\" Either escapes special characters or signals a special sequence.
+ [] Indicates a set of characters.
+ A "^" as the first character indicates a complementing set.
+ "|" A|B, creates an RE that will match either A or B.
+ (...) Matches the RE inside the parentheses.
+ The contents can be retrieved or matched later in the string.
+ (?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below).
+ (?:...) Non-grouping version of regular parentheses.
+ (?P<name>...) The substring matched by the group is accessible by name.
+ (?P=name) Matches the text matched earlier by the group named name.
+ (?#...) A comment; ignored.
+ (?=...) Matches if ... matches next, but doesn't consume the string.
+ (?!...) Matches if ... doesn't match next.
+ (?<=...) Matches if preceded by ... (must be fixed length).
+ (?<!...) Matches if not preceded by ... (must be fixed length).
+ (?(id/name)yes|no) Matches yes pattern if the group with id/name matched,
+ the (optional) no pattern otherwise.
+
+The special sequences consist of "\\" and a character from the list
+below. If the ordinary character is not on the list, then the
+resulting RE will match the second character.
+ \number Matches the contents of the group of the same number.
+ \A Matches only at the start of the string.
+ \Z Matches only at the end of the string.
+ \b Matches the empty string, but only at the start or end of a word.
+ \B Matches the empty string, but not at the start or end of a word.
+ \d Matches any decimal digit; equivalent to the set [0-9] in
+ bytes patterns or string patterns with the ASCII flag.
+ In string patterns without the ASCII flag, it will match the whole
+ range of Unicode digits.
+ \D Matches any non-digit character; equivalent to [^\d].
+ \s Matches any whitespace character; equivalent to [ \t\n\r\f\v] in
+ bytes patterns or string patterns with the ASCII flag.
+ In string patterns without the ASCII flag, it will match the whole
+ range of Unicode whitespace characters.
+ \S Matches any non-whitespace character; equivalent to [^\s].
+ \w Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]
+ in bytes patterns or string patterns with the ASCII flag.
+ In string patterns without the ASCII flag, it will match the
+ range of Unicode alphanumeric characters (letters plus digits
+ plus underscore).
+ With LOCALE, it will match the set [0-9_] plus characters defined
+ as letters for the current locale.
+ \W Matches the complement of \w.
+ \\ Matches a literal backslash.
+
+This module exports the following functions:
+ match Match a regular expression pattern to the beginning of a string.
+ search Search a string for the presence of a pattern.
+ sub Substitute occurrences of a pattern found in a string.
+ subn Same as sub, but also return the number of substitutions made.
+ split Split a string by the occurrences of a pattern.
+ findall Find all occurrences of a pattern in a string.
+ finditer Return an iterator yielding a match object for each match.
+ compile Compile a pattern into a RegexObject.
+ purge Clear the regular expression cache.
+ escape Backslash all non-alphanumerics in a string.
+
+Some of the functions in this module takes flags as optional parameters:
+ A ASCII For string patterns, make \w, \W, \b, \B, \d, \D
+ match the corresponding ASCII character categories
+ (rather than the whole Unicode categories, which is the
+ default).
+ For bytes patterns, this flag is the only available
+ behaviour and needn't be specified.
+ I IGNORECASE Perform case-insensitive matching.
+ L LOCALE Make \w, \W, \b, \B, dependent on the current locale.
+ M MULTILINE "^" matches the beginning of lines (after a newline)
+ as well as the string.
+ "$" matches the end of lines (before a newline) as well
+ as the end of the string.
+ S DOTALL "." matches any character at all, including the newline.
+ X VERBOSE Ignore whitespace and comments for nicer looking RE's.
+ U UNICODE For compatibility only. Ignored for string patterns (it
+ is the default), and forbidden for bytes patterns.
+
+This module also defines an exception 'error'.
+
+"""
+
+import sys
+from . import sre_compile
+from . import sre_parse
+import functools
+
+# public symbols
+__all__ = [ "match", "search", "sub", "subn", "split", "findall",
+ "compile", "purge", "template", "escape", "A", "I", "L", "M", "S", "X",
+ "U", "ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
+ "UNICODE", "error" ]
+
+__version__ = "2.2.1"
+
+# flags
+A = ASCII = sre_compile.SRE_FLAG_ASCII # assume ascii "locale"
+I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case
+L = LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale
+U = UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode "locale"
+M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline
+S = DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline
+X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments
+
+# sre extensions (experimental, don't rely on these)
+T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
+DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation
+
+# sre exception
+error = sre_compile.error
+
+# --------------------------------------------------------------------
+# public interface
+
+def match(pattern, string, flags=0):
+ """Try to apply the pattern at the start of the string, returning
+ a match object, or None if no match was found."""
+ return _compile(pattern, flags).match(string)
+
+def search(pattern, string, flags=0):
+ """Scan through string looking for a match to the pattern, returning
+ a match object, or None if no match was found."""
+ return _compile(pattern, flags).search(string)
+
+def sub(pattern, repl, string, count=0, flags=0):
+ """Return the string obtained by replacing the leftmost
+ non-overlapping occurrences of the pattern in string by the
+ replacement repl. repl can be either a string or a callable;
+ if a string, backslash escapes in it are processed. If it is
+ a callable, it's passed the match object and must return
+ a replacement string to be used."""
+ return _compile(pattern, flags).sub(repl, string, count)
+
+def subn(pattern, repl, string, count=0, flags=0):
+ """Return a 2-tuple containing (new_string, number).
+ new_string is the string obtained by replacing the leftmost
+ non-overlapping occurrences of the pattern in the source
+ string by the replacement repl. number is the number of
+ substitutions that were made. repl can be either a string or a
+ callable; if a string, backslash escapes in it are processed.
+ If it is a callable, it's passed the match object and must
+ return a replacement string to be used."""
+ return _compile(pattern, flags).subn(repl, string, count)
+
+def split(pattern, string, maxsplit=0, flags=0):
+ """Split the source string by the occurrences of the pattern,
+ returning a list containing the resulting substrings. If
+ capturing parentheses are used in pattern, then the text of all
+ groups in the pattern are also returned as part of the resulting
+ list. If maxsplit is nonzero, at most maxsplit splits occur,
+ and the remainder of the string is returned as the final element
+ of the list."""
+ return _compile(pattern, flags).split(string, maxsplit)
+
+def findall(pattern, string, flags=0):
+ """Return a list of all non-overlapping matches in the string.
+
+ If one or more capturing groups are present in the pattern, return
+ a list of groups; this will be a list of tuples if the pattern
+ has more than one group.
+
+ Empty matches are included in the result."""
+ return _compile(pattern, flags).findall(string)
+
+if sys.hexversion >= 0x02020000:
+ __all__.append("finditer")
+ def finditer(pattern, string, flags=0):
+ """Return an iterator over all non-overlapping matches in the
+ string. For each match, the iterator returns a match object.
+
+ Empty matches are included in the result."""
+ return _compile(pattern, flags).finditer(string)
+
+def compile(pattern, flags=0):
+ "Compile a regular expression pattern, returning a pattern object."
+ return _compile(pattern, flags)
+
+def purge():
+ "Clear the regular expression caches"
+ _cache.clear()
+ _cache_repl.clear()
+
+def template(pattern, flags=0):
+ "Compile a template pattern, returning a pattern object"
+ return _compile(pattern, flags|T)
+
+_alphanum_str = frozenset(
+ "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890")
+_alphanum_bytes = frozenset(
+ b"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890")
+
+def escape(pattern):
+ """
+ Escape all the characters in pattern except ASCII letters, numbers and '_'.
+ """
+ if isinstance(pattern, str):
+ alphanum = _alphanum_str
+ s = list(pattern)
+ for i, c in enumerate(pattern):
+ if c not in alphanum:
+ if c == "\000":
+ s[i] = "\\000"
+ else:
+ s[i] = "\\" + c
+ return "".join(s)
+ else:
+ alphanum = _alphanum_bytes
+ s = []
+ esc = ord(b"\\")
+ for c in pattern:
+ if c in alphanum:
+ s.append(c)
+ else:
+ if c == 0:
+ s.extend(b"\\000")
+ else:
+ s.append(esc)
+ s.append(c)
+ return bytes(s)
+
+# --------------------------------------------------------------------
+# internals
+
+_cache = {}
+_cache_repl = {}
+
+_pattern_type = type(sre_compile.compile("", 0))
+
+_MAXCACHE = 512
+def _compile(pattern, flags):
+ # internal: compile pattern
+ try:
+ return _cache[type(pattern), pattern, flags]
+ except KeyError:
+ pass
+ if isinstance(pattern, _pattern_type):
+ if flags:
+ raise ValueError(
+ "Cannot process flags argument with a compiled pattern")
+ return pattern
+ if not sre_compile.isstring(pattern):
+ raise TypeError("first argument must be string or compiled pattern")
+ p = sre_compile.compile(pattern, flags)
+ if len(_cache) >= _MAXCACHE:
+ _cache.clear()
+ _cache[type(pattern), pattern, flags] = p
+ return p
+
+def _compile_repl(repl, pattern):
+ # internal: compile replacement pattern
+ try:
+ return _cache_repl[repl, pattern]
+ except KeyError:
+ pass
+ p = sre_parse.parse_template(repl, pattern)
+ if len(_cache_repl) >= _MAXCACHE:
+ _cache_repl.clear()
+ _cache_repl[repl, pattern] = p
+ return p
+
+def _expand(pattern, match, template):
+ # internal: match.expand implementation hook
+ template = sre_parse.parse_template(template, pattern)
+ return sre_parse.expand_template(template, match)
+
+def _subx(pattern, template):
+ # internal: pattern.sub/subn implementation helper
+ template = _compile_repl(template, pattern)
+ if not template[0] and len(template[1]) == 1:
+ # literal replacement
+ return template[1][0]
+ def filter(match, template=template):
+ return sre_parse.expand_template(template, match)
+ return filter
+
+# register myself for pickling
+
+import copyreg
+
+def _pickle(p):
+ return _compile, (p.pattern, p.flags)
+
+copyreg.pickle(_pattern_type, _pickle, _compile)
+
+# --------------------------------------------------------------------
+# experimental stuff (see python-dev discussions for details)
+
+class Scanner:
+ def __init__(self, lexicon, flags=0):
+ from sre_constants import BRANCH, SUBPATTERN
+ self.lexicon = lexicon
+ # combine phrases into a compound pattern
+ p = []
+ s = sre_parse.Pattern()
+ s.flags = flags
+ for phrase, action in lexicon:
+ p.append(sre_parse.SubPattern(s, [
+ (SUBPATTERN, (len(p)+1, sre_parse.parse(phrase, flags))),
+ ]))
+ s.groups = len(p)+1
+ p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
+ self.scanner = sre_compile.compile(p)
+ def scan(self, string):
+ result = []
+ append = result.append
+ match = self.scanner.scanner(string).match
+ i = 0
+ while 1:
+ m = match()
+ if not m:
+ break
+ j = m.end()
+ if i == j:
+ break
+ action = self.lexicon[m.lastindex-1][1]
+ if callable(action):
+ self.match = m
+ action = action(self, m.group())
+ if action is not None:
+ append(action)
+ i = j
+ return result, string[i:]
521 build/lib.macosx-10.8-x86_64-3.3/gappedbuf/sre_compile.py
@@ -0,0 +1,521 @@
+#
+# Secret Labs' Regular Expression Engine
+#
+# convert template to internal format
+#
+# Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
+#
+# See the sre.py file for information on usage and redistribution.
+#
+
+"""Internal support module for sre"""
+
+import sys
+import _gappedbuf
+_gappedbuf.init_gappedbufre()
+import _gappedbufre as _sre
+from . import sre_parse
+from .sre_constants import *
+from _gappedbufre import MAXREPEAT
+
+assert _sre.MAGIC == MAGIC, "SRE module mismatch"
+
+if _sre.CODESIZE == 2:
+ MAXCODE = 65535
+else:
+ MAXCODE = 0xFFFFFFFF
+
+def _identityfunction(x):
+ return x
+
+_LITERAL_CODES = set([LITERAL, NOT_LITERAL])
+_REPEATING_CODES = set([REPEAT, MIN_REPEAT, MAX_REPEAT])
+_SUCCESS_CODES = set([SUCCESS, FAILURE])
+_ASSERT_CODES = set([ASSERT, ASSERT_NOT])
+
+def _compile(code, pattern, flags):
+ # internal: compile a (sub)pattern
+ emit = code.append
+ _len = len
+ LITERAL_CODES = _LITERAL_CODES
+ REPEATING_CODES = _REPEATING_CODES
+ SUCCESS_CODES = _SUCCESS_CODES
+ ASSERT_CODES = _ASSERT_CODES
+ for op, av in pattern:
+ if op in LITERAL_CODES:
+ if flags & SRE_FLAG_IGNORECASE:
+ emit(OPCODES[OP_IGNORE[op]])
+ emit(_sre.getlower(av, flags))
+ else:
+ emit(OPCODES[op])
+ emit(av)
+ elif op is IN:
+ if flags & SRE_FLAG_IGNORECASE:
+ emit(OPCODES[OP_IGNORE[op]])
+ def fixup(literal, flags=flags):
+ return _sre.getlower(literal, flags)
+ else:
+ emit(OPCODES[op])
+ fixup = _identityfunction
+ skip = _len(code); emit(0)
+ _compile_charset(av, flags, code, fixup)
+ code[skip] = _len(code) - skip
+ elif op is ANY:
+ if flags & SRE_FLAG_DOTALL:
+ emit(OPCODES[ANY_ALL])
+ else:
+ emit(OPCODES[ANY])
+ elif op in REPEATING_CODES:
+ if flags & SRE_FLAG_TEMPLATE:
+ raise error("internal: unsupported template operator")
+ emit(OPCODES[REPEAT])
+ skip = _len(code); emit(0)
+ emit(av[0])
+ emit(av[1])
+ _compile(code, av[2], flags)
+ emit(OPCODES[SUCCESS])
+ code[skip] = _len(code) - skip
+ elif _simple(av) and op is not REPEAT:
+ if op is MAX_REPEAT:
+ emit(OPCODES[REPEAT_ONE])
+ else:
+ emit(OPCODES[MIN_REPEAT_ONE])
+ skip = _len(code); emit(0)
+ emit(av[0])
+ emit(av[1])
+ _compile(code, av[2], flags)
+ emit(OPCODES[SUCCESS])
+ code[skip] = _len(code) - skip
+ else:
+ emit(OPCODES[REPEAT])
+ skip = _len(code); emit(0)
+ emit(av[0])
+ emit(av[1])
+ _compile(code, av[2], flags)
+ code[skip] = _len(code) - skip
+ if op is MAX_REPEAT:
+ emit(OPCODES[MAX_UNTIL])
+ else:
+ emit(OPCODES[MIN_UNTIL])
+ elif op is SUBPATTERN:
+ if av[0]:
+ emit(OPCODES[MARK])
+ emit((av[0]-1)*2)
+ # _compile_info(code, av[1], flags)
+ _compile(code, av[1], flags)
+ if av[0]:
+ emit(OPCODES[MARK])
+ emit((av[0]-1)*2+1)
+ elif op in SUCCESS_CODES:
+ emit(OPCODES[op])
+ elif op in ASSERT_CODES:
+ emit(OPCODES[op])
+ skip = _len(code); emit(0)
+ if av[0] >= 0:
+ emit(0) # look ahead
+ else:
+ lo, hi = av[1].getwidth()
+ if lo != hi:
+ raise error("look-behind requires fixed-width pattern")
+ emit(lo) # look behind
+ _compile(code, av[1], flags)
+ emit(OPCODES[SUCCESS])
+ code[skip] = _len(code) - skip
+ elif op is CALL:
+ emit(OPCODES[op])
+ skip = _len(code); emit(0)
+ _compile(code, av, flags)
+ emit(OPCODES[SUCCESS])
+ code[skip] = _len(code) - skip
+ elif op is AT:
+ emit(OPCODES[op])
+ if flags & SRE_FLAG_MULTILINE:
+ av = AT_MULTILINE.get(av, av)
+ if flags & SRE_FLAG_LOCALE:
+ av = AT_LOCALE.get(av, av)
+ elif flags & SRE_FLAG_UNICODE:
+ av = AT_UNICODE.get(av, av)
+ emit(ATCODES[av])
+ elif op is BRANCH:
+ emit(OPCODES[op])
+ tail = []
+ tailappend = tail.append
+ for av in av[1]:
+ skip = _len(code); emit(0)
+ # _compile_info(code, av, flags)
+ _compile(code, av, flags)
+ emit(OPCODES[JUMP])
+ tailappend(_len(code)); emit(0)
+ code[skip] = _len(code) - skip
+ emit(0) # end of branch
+ for tail in tail:
+ code[tail] = _len(code) - tail
+ elif op is CATEGORY:
+ emit(OPCODES[op])
+ if flags & SRE_FLAG_LOCALE:
+ av = CH_LOCALE[av]
+ elif flags & SRE_FLAG_UNICODE:
+ av = CH_UNICODE[av]
+ emit(CHCODES[av])
+ elif op is GROUPREF:
+ if flags & SRE_FLAG_IGNORECASE:
+ emit(OPCODES[OP_IGNORE[op]])
+ else:
+ emit(OPCODES[op])
+ emit(av-1)
+ elif op is GROUPREF_EXISTS:
+ emit(OPCODES[op])
+ emit(av[0]-1)
+ skipyes = _len(code); emit(0)
+ _compile(code, av[1], flags)
+ if av[2]:
+ emit(OPCODES[JUMP])
+ skipno = _len(code); emit(0)
+ code[skipyes] = _len(code) - skipyes + 1
+ _compile(code, av[2], flags)
+ code[skipno] = _len(code) - skipno
+ else:
+ code[skipyes] = _len(code) - skipyes + 1
+ else:
+ raise ValueError("unsupported operand type", op)
+
+def _compile_charset(charset, flags, code, fixup=None):
+ # compile charset subprogram
+ emit = code.append
+ if fixup is None:
+ fixup = _identityfunction
+ for op, av in _optimize_charset(charset, fixup):
+ emit(OPCODES[op])
+ if op is NEGATE:
+ pass
+ elif op is LITERAL:
+ emit(fixup(av))
+ elif op is RANGE:
+ emit(fixup(av[0]))
+ emit(fixup(av[1]))
+ elif op is CHARSET:
+ code.extend(av)
+ elif op is BIGCHARSET:
+ code.extend(av)
+ elif op is CATEGORY:
+ if flags & SRE_FLAG_LOCALE:
+ emit(CHCODES[CH_LOCALE[av]])
+ elif flags & SRE_FLAG_UNICODE:
+ emit(CHCODES[CH_UNICODE[av]])
+ else:
+ emit(CHCODES[av])
+ else:
+ raise error("internal: unsupported set operator")
+ emit(OPCODES[FAILURE])
+
+def _optimize_charset(charset, fixup):
+ # internal: optimize character set
+ out = []
+ outappend = out.append
+ charmap = [0]*256
+ try:
+ for op, av in charset:
+ if op is NEGATE:
+ outappend((op, av))
+ elif op is LITERAL:
+ charmap[fixup(av)] = 1
+ elif op is RANGE:
+ for i in range(fixup(av[0]), fixup(av[1])+1):
+ charmap[i] = 1
+ elif op is CATEGORY:
+ # XXX: could append to charmap tail
+ return charset # cannot compress
+ except IndexError:
+ # character set contains unicode characters
+ return _optimize_unicode(charset, fixup)
+ # compress character map
+ i = p = n = 0
+ runs = []
+ runsappend = runs.append
+ for c in charmap:
+ if c:
+ if n == 0:
+ p = i
+ n = n + 1
+ elif n:
+ runsappend((p, n))
+ n = 0
+ i = i + 1
+ if n:
+ runsappend((p, n))
+ if len(runs) <= 2:
+ # use literal/range
+ for p, n in runs:
+ if n == 1:
+ outappend((LITERAL, p))
+ else:
+ outappend((RANGE, (p, p+n-1)))
+ if len(out) < len(charset):
+ return out
+ else:
+ # use bitmap
+ data = _mk_bitmap(charmap)
+ outappend((CHARSET, data))
+ return out
+ return charset
+
+def _mk_bitmap(bits):
+ data = []
+ dataappend = data.append
+ if _sre.CODESIZE == 2:
+ start = (1, 0)
+ else:
+ start = (1, 0)
+ m, v = start
+ for c in bits:
+ if c:
+ v = v + m
+ m = m + m
+ if m > MAXCODE:
+ dataappend(v)
+ m, v = start
+ return data
+
+# To represent a big charset, first a bitmap of all characters in the
+# set is constructed. Then, this bitmap is sliced into chunks of 256
+# characters, duplicate chunks are eliminated, and each chunk is
+# given a number. In the compiled expression, the charset is
+# represented by a 16-bit word sequence, consisting of one word for
+# the number of different chunks, a sequence of 256 bytes (128 words)
+# of chunk numbers indexed by their original chunk position, and a
+# sequence of chunks (16 words each).
+
+# Compression is normally good: in a typical charset, large ranges of
+# Unicode will be either completely excluded (e.g. if only cyrillic
+# letters are to be matched), or completely included (e.g. if large
+# subranges of Kanji match). These ranges will be represented by
+# chunks of all one-bits or all zero-bits.
+
+# Matching can be also done efficiently: the more significant byte of
+# the Unicode character is an index into the chunk number, and the
+# less significant byte is a bit index in the chunk (just like the
+# CHARSET matching).
+
+# In UCS-4 mode, the BIGCHARSET opcode still supports only subsets
+# of the basic multilingual plane; an efficient representation
+# for all of UTF-16 has not yet been developed. This means,
+# in particular, that negated charsets cannot be represented as
+# bigcharsets.
+
+def _optimize_unicode(charset, fixup):
+ try:
+ import array
+ except ImportError:
+ return charset
+ charmap = [0]*65536
+ negate = 0
+ try:
+ for op, av in charset:
+ if op is NEGATE:
+ negate = 1
+ elif op is LITERAL:
+ charmap[fixup(av)] = 1
+ elif op is RANGE:
+ for i in range(fixup(av[0]), fixup(av[1])+1):
+ charmap[i] = 1
+ elif op is CATEGORY:
+ # XXX: could expand category
+ return charset # cannot compress
+ except IndexError:
+ # non-BMP characters; XXX now they should work
+ return charset
+ if negate:
+ if sys.maxunicode != 65535:
+ # XXX: negation does not work with big charsets
+ # XXX2: now they should work, but removing this will make the
+ # charmap 17 times bigger
+ return charset
+ for i in range(65536):
+ charmap[i] = not charmap[i]
+ comps = {}
+ mapping = [0]*256
+ block = 0
+ data = []
+ for i in range(256):
+ chunk = tuple(charmap[i*256:(i+1)*256])
+ new = comps.setdefault(chunk, block)
+ mapping[i] = new
+ if new == block:
+ block = block + 1
+ data = data + _mk_bitmap(chunk)
+ header = [block]
+ if _sre.CODESIZE == 2:
+ code = 'H'
+ else:
+ code = 'I'
+ # Convert block indices to byte array of 256 bytes
+ mapping = array.array('b', mapping).tobytes()
+ # Convert byte array to word array
+ mapping = array.array(code, mapping)
+ assert mapping.itemsize == _sre.CODESIZE
+ assert len(mapping) * mapping.itemsize == 256
+ header = header + mapping.tolist()
+ data[0:0] = header
+ return [(BIGCHARSET, data)]
+
+def _simple(av):
+ # check if av is a "simple" operator
+ lo, hi = av[2].getwidth()
+ if lo == 0 and hi == MAXREPEAT:
+ raise error("nothing to repeat")
+ return lo == hi == 1 and av[2][0][0] != SUBPATTERN
+
+def _compile_info(code, pattern, flags):
+ # internal: compile an info block. in the current version,
+ # this contains min/max pattern width, and an optional literal
+ # prefix or a character map
+ lo, hi = pattern.getwidth()
+ if lo == 0:
+ return # not worth it
+ # look for a literal prefix
+ prefix = []
+ prefixappend = prefix.append
+ prefix_skip = 0
+ charset = [] # not used
+ charsetappend = charset.append
+ if not (flags & SRE_FLAG_IGNORECASE):
+ # look for literal prefix
+ for op, av in pattern.data:
+ if op is LITERAL:
+ if len(prefix) == prefix_skip:
+ prefix_skip = prefix_skip + 1
+ prefixappend(av)
+ elif op is SUBPATTERN and len(av[1]) == 1:
+ op, av = av[1][0]
+ if op is LITERAL:
+ prefixappend(av)
+ else:
+ break
+ else:
+ break
+ # if no prefix, look for charset prefix
+ if not prefix and pattern.data:
+ op, av = pattern.data[0]
+ if op is SUBPATTERN and av[1]:
+ op, av = av[1][0]
+ if op is LITERAL:
+ charsetappend((op, av))
+ elif op is BRANCH:
+ c = []
+ cappend = c.append
+ for p in av[1]:
+ if not p:
+ break
+ op, av = p[0]
+ if op is LITERAL:
+ cappend((op, av))
+ else:
+ break
+ else:
+ charset = c
+ elif op is BRANCH:
+ c = []
+ cappend = c.append
+ for p in av[1]:
+ if not p:
+ break
+ op, av = p[0]
+ if op is LITERAL:
+ cappend((op, av))
+ else:
+ break
+ else:
+ charset = c
+ elif op is IN:
+ charset = av
+## if prefix:
+## print "*** PREFIX", prefix, prefix_skip
+## if charset:
+## print "*** CHARSET", charset
+ # add an info block
+ emit = code.append
+ emit(OPCODES[INFO])
+ skip = len(code); emit(0)
+ # literal flag
+ mask = 0
+ if prefix:
+ mask = SRE_INFO_PREFIX
+ if len(prefix) == prefix_skip == len(pattern.data):
+ mask = mask + SRE_INFO_LITERAL
+ elif charset:
+ mask = mask + SRE_INFO_CHARSET
+ emit(mask)
+ # pattern length
+ if lo < MAXCODE:
+ emit(lo)
+ else:
+ emit(MAXCODE)
+ prefix = prefix[:MAXCODE]
+ if hi < MAXCODE:
+ emit(hi)
+ else:
+ emit(0)
+ # add literal prefix
+ if prefix:
+ emit(len(prefix)) # length
+ emit(prefix_skip) # skip
+ code.extend(prefix)
+ # generate overlap table
+ table = [-1] + ([0]*len(prefix))
+ for i in range(len(prefix)):
+ table[i+1] = table[i]+1
+ while table[i+1] > 0 and prefix[i] != prefix[table[i+1]-1]:
+ table[i+1] = table[table[i+1]-1]+1
+ code.extend(table[1:]) # don't store first entry
+ elif charset:
+ _compile_charset(charset, flags, code)
+ code[skip] = len(code) - skip
+
+def isstring(obj):
+ return isinstance(obj, (str, bytes))
+
+def _code(p, flags):
+
+ flags = p.pattern.flags | flags
+ code = []
+
+ # compile info block
+ _compile_info(code, p, flags)
+
+ # compile the pattern
+ _compile(code, p.data, flags)
+
+ code.append(OPCODES[SUCCESS])
+
+ return code
+
+def compile(p, flags=0):
+ # internal: convert pattern list to internal format
+
+ if isstring(p):
+ pattern = p
+ p = sre_parse.parse(p, flags)
+ else:
+ pattern = None
+
+ code = _code(p, flags)
+
+ # print code
+
+ # XXX: <fl> get rid of this limitation!
+ if p.pattern.groups > 100:
+ raise AssertionError(
+ "sorry, but this version only supports 100 named groups"
+ )
+
+ # map in either direction
+ groupindex = p.pattern.groupdict
+ indexgroup = [None] * p.pattern.groups
+ for k, i in groupindex.items():
+ indexgroup[i] = k
+
+ return _sre.compile(
+ pattern, flags | p.pattern.flags, code,
+ p.pattern.groups-1,
+ groupindex, indexgroup
+ )
259 build/lib.macosx-10.8-x86_64-3.3/gappedbuf/sre_constants.py
@@ -0,0 +1,259 @@
+#
+# Secret Labs' Regular Expression Engine
+#
+# various symbols used by the regular expression engine.
+# run this script to update the _sre include files!
+#
+# Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved.
+#
+# See the sre.py file for information on usage and redistribution.
+#
+
+"""Internal support module for sre"""
+
+# update when constants are added or removed
+
+MAGIC = 20031017
+
+from _gappedbufre import MAXREPEAT
+
+# SRE standard exception (access as sre.error)
+# should this really be here?
+
+class error(Exception):
+ pass
+
+# operators
+
+FAILURE = "failure"
+SUCCESS = "success"
+
+ANY = "any"
+ANY_ALL = "any_all"
+ASSERT = "assert"
+ASSERT_NOT = "assert_not"
+AT = "at"
+BIGCHARSET = "bigcharset"
+BRANCH = "branch"
+CALL = "call"
+CATEGORY = "category"
+CHARSET = "charset"
+GROUPREF = "groupref"
+GROUPREF_IGNORE = "groupref_ignore"
+GROUPREF_EXISTS = "groupref_exists"
+IN = "in"
+IN_IGNORE = "in_ignore"
+INFO = "info"
+JUMP = "jump"
+LITERAL = "literal"
+LITERAL_IGNORE = "literal_ignore"
+MARK = "mark"
+MAX_REPEAT = "max_repeat"
+MAX_UNTIL = "max_until"
+MIN_REPEAT = "min_repeat"
+MIN_UNTIL = "min_until"
+NEGATE = "negate"
+NOT_LITERAL = "not_literal"
+NOT_LITERAL_IGNORE = "not_literal_ignore"
+RANGE = "range"
+REPEAT = "repeat"
+REPEAT_ONE = "repeat_one"
+SUBPATTERN = "subpattern"
+MIN_REPEAT_ONE = "min_repeat_one"
+
+# positions
+AT_BEGINNING = "at_beginning"
+AT_BEGINNING_LINE = "at_beginning_line"
+AT_BEGINNING_STRING = "at_beginning_string"
+AT_BOUNDARY = "at_boundary"
+AT_NON_BOUNDARY = "at_non_boundary"
+AT_END = "at_end"
+AT_END_LINE = "at_end_line"
+AT_END_STRING = "at_end_string"
+AT_LOC_BOUNDARY = "at_loc_boundary"
+AT_LOC_NON_BOUNDARY = "at_loc_non_boundary"
+AT_UNI_BOUNDARY = "at_uni_boundary"
+AT_UNI_NON_BOUNDARY = "at_uni_non_boundary"
+
+# categories
+CATEGORY_DIGIT = "category_digit"
+CATEGORY_NOT_DIGIT = "category_not_digit"
+CATEGORY_SPACE = "category_space"
+CATEGORY_NOT_SPACE = "category_not_space"
+CATEGORY_WORD = "category_word"
+CATEGORY_NOT_WORD = "category_not_word"
+CATEGORY_LINEBREAK = "category_linebreak"
+CATEGORY_NOT_LINEBREAK = "category_not_linebreak"
+CATEGORY_LOC_WORD = "category_loc_word"
+CATEGORY_LOC_NOT_WORD = "category_loc_not_word"
+CATEGORY_UNI_DIGIT = "category_uni_digit"
+CATEGORY_UNI_NOT_DIGIT = "category_uni_not_digit"
+CATEGORY_UNI_SPACE = "category_uni_space"
+CATEGORY_UNI_NOT_SPACE = "category_uni_not_space"
+CATEGORY_UNI_WORD = "category_uni_word"
+CATEGORY_UNI_NOT_WORD = "category_uni_not_word"
+CATEGORY_UNI_LINEBREAK = "category_uni_linebreak"
+CATEGORY_UNI_NOT_LINEBREAK = "category_uni_not_linebreak"
+
+OPCODES = [
+
+ # failure=0 success=1 (just because it looks better that way :-)
+ FAILURE, SUCCESS,
+
+ ANY, ANY_ALL,
+ ASSERT, ASSERT_NOT,
+ AT,
+ BRANCH,
+ CALL,
+ CATEGORY,
+ CHARSET, BIGCHARSET,
+ GROUPREF, GROUPREF_EXISTS, GROUPREF_IGNORE,
+ IN, IN_IGNORE,
+ INFO,
+ JUMP,
+ LITERAL, LITERAL_IGNORE,
+ MARK,
+ MAX_UNTIL,
+ MIN_UNTIL,
+ NOT_LITERAL, NOT_LITERAL_IGNORE,
+ NEGATE,
+ RANGE,
+ REPEAT,
+ REPEAT_ONE,
+ SUBPATTERN,
+ MIN_REPEAT_ONE
+
+]
+
+ATCODES = [
+ AT_BEGINNING, AT_BEGINNING_LINE, AT_BEGINNING_STRING, AT_BOUNDARY,
+ AT_NON_BOUNDARY, AT_END, AT_END_LINE, AT_END_STRING,
+ AT_LOC_BOUNDARY, AT_LOC_NON_BOUNDARY, AT_UNI_BOUNDARY,
+ AT_UNI_NON_BOUNDARY
+]
+
+CHCODES = [
+ CATEGORY_DIGIT, CATEGORY_NOT_DIGIT, CATEGORY_SPACE,
+ CATEGORY_NOT_SPACE, CATEGORY_WORD, CATEGORY_NOT_WORD,
+ CATEGORY_LINEBREAK, CATEGORY_NOT_LINEBREAK, CATEGORY_LOC_WORD,
+ CATEGORY_LOC_NOT_WORD, CATEGORY_UNI_DIGIT, CATEGORY_UNI_NOT_DIGIT,
+ CATEGORY_UNI_SPACE, CATEGORY_UNI_NOT_SPACE, CATEGORY_UNI_WORD,
+ CATEGORY_UNI_NOT_WORD, CATEGORY_UNI_LINEBREAK,
+ CATEGORY_UNI_NOT_LINEBREAK
+]
+
+def makedict(list):
+ d = {}
+ i = 0
+ for item in list:
+ d[item] = i
+ i = i + 1
+ return d
+
+OPCODES = makedict(OPCODES)
+ATCODES = makedict(ATCODES)
+CHCODES = makedict(CHCODES)
+
+# replacement operations for "ignore case" mode
+OP_IGNORE = {
+ GROUPREF: GROUPREF_IGNORE,
+ IN: IN_IGNORE,
+ LITERAL: LITERAL_IGNORE,
+ NOT_LITERAL: NOT_LITERAL_IGNORE
+}
+
+AT_MULTILINE = {
+ AT_BEGINNING: AT_BEGINNING_LINE,
+ AT_END: AT_END_LINE
+}
+
+AT_LOCALE = {
+ AT_BOUNDARY: AT_LOC_BOUNDARY,
+ AT_NON_BOUNDARY: AT_LOC_NON_BOUNDARY
+}
+
+AT_UNICODE = {
+ AT_BOUNDARY: AT_UNI_BOUNDARY,
+ AT_NON_BOUNDARY: AT_UNI_NON_BOUNDARY
+}
+
+CH_LOCALE = {
+ CATEGORY_DIGIT: CATEGORY_DIGIT,
+ CATEGORY_NOT_DIGIT: CATEGORY_NOT_DIGIT,
+ CATEGORY_SPACE: CATEGORY_SPACE,
+ CATEGORY_NOT_SPACE: CATEGORY_NOT_SPACE,
+ CATEGORY_WORD: CATEGORY_LOC_WORD,
+ CATEGORY_NOT_WORD: CATEGORY_LOC_NOT_WORD,
+ CATEGORY_LINEBREAK: CATEGORY_LINEBREAK,
+ CATEGORY_NOT_LINEBREAK: CATEGORY_NOT_LINEBREAK
+}
+
+CH_UNICODE = {
+ CATEGORY_DIGIT: CATEGORY_UNI_DIGIT,
+ CATEGORY_NOT_DIGIT: CATEGORY_UNI_NOT_DIGIT,
+ CATEGORY_SPACE: CATEGORY_UNI_SPACE,
+ CATEGORY_NOT_SPACE: CATEGORY_UNI_NOT_SPACE,
+ CATEGORY_WORD: CATEGORY_UNI_WORD,
+ CATEGORY_NOT_WORD: CATEGORY_UNI_NOT_WORD,
+ CATEGORY_LINEBREAK: CATEGORY_UNI_LINEBREAK,
+ CATEGORY_NOT_LINEBREAK: CATEGORY_UNI_NOT_LINEBREAK
+}
+
+# flags
+SRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking)
+SRE_FLAG_IGNORECASE = 2 # case insensitive
+SRE_FLAG_LOCALE = 4 # honour system locale
+SRE_FLAG_MULTILINE = 8 # treat target as multiline string
+SRE_FLAG_DOTALL = 16 # treat target as a single string
+SRE_FLAG_UNICODE = 32 # use unicode "locale"
+SRE_FLAG_VERBOSE = 64 # ignore whitespace and comments
+SRE_FLAG_DEBUG = 128 # debugging
+SRE_FLAG_ASCII = 256 # use ascii "locale"
+
+# flags for INFO primitive
+SRE_INFO_PREFIX = 1 # has prefix
+SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix)
+SRE_INFO_CHARSET = 4 # pattern starts with character from given set
+
+if __name__ == "__main__":
+ def dump(f, d, prefix):
+ items = sorted(d.items(), key=lambda a: a[1])
+ for k, v in items:
+ f.write("#define %s_%s %s\n" % (prefix, k.upper(), v))
+ f = open("sre_constants.h", "w")
+ f.write("""\
+/*
+ * Secret Labs' Regular Expression Engine
+ *
+ * regular expression matching engine
+ *
+ * NOTE: This file is generated by sre_constants.py. If you need
+ * to change anything in here, edit sre_constants.py and run it.
+ *
+ * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
+ *
+ * See the _sre.c file for information on usage and redistribution.
+ */
+
+""")
+
+ f.write("#define SRE_MAGIC %d\n" % MAGIC)
+
+ dump(f, OPCODES, "SRE_OP")
+ dump(f, ATCODES, "SRE")
+ dump(f, CHCODES, "SRE")
+
+ f.write("#define SRE_FLAG_TEMPLATE %d\n" % SRE_FLAG_TEMPLATE)
+ f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE)
+ f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE)
+ f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE)
+ f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL)
+ f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE)
+ f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE)
+
+ f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX)
+ f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL)
+ f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET)
+
+ f.close()
+ print("done")
859 build/lib.macosx-10.8-x86_64-3.3/gappedbuf/sre_parse.py
@@ -0,0 +1,859 @@
+#
+# Secret Labs' Regular Expression Engine
+#
+# convert re-style regular expression to sre pattern
+#
+# Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved.
+#
+# See the sre.py file for information on usage and redistribution.
+#
+
+"""Internal support module for sre"""
+
+# XXX: show string offset and offending character for all errors
+
+import sys
+
+from .sre_constants import *
+from _gappedbufre import MAXREPEAT
+
+SPECIAL_CHARS = ".\\[{()*+?^$|"
+REPEAT_CHARS = "*+?{"
+
+DIGITS = set("0123456789")
+
+OCTDIGITS = set("01234567")
+HEXDIGITS = set("0123456789abcdefABCDEF")
+
+WHITESPACE = set(" \t\n\r\v\f")
+
+ESCAPES = {
+ r"\a": (LITERAL, ord("\a")),
+ r"\b": (LITERAL, ord("\b")),
+ r"\f": (LITERAL, ord("\f")),
+ r"\n": (LITERAL, ord("\n")),
+ r"\r": (LITERAL, ord("\r")),
+ r"\t": (LITERAL, ord("\t")),
+ r"\v": (LITERAL, ord("\v")),
+ r"\\": (LITERAL, ord("\\"))
+}
+
+CATEGORIES = {
+ r"\A": (AT, AT_BEGINNING_STRING), # start of string
+ r"\b": (AT, AT_BOUNDARY),
+ r"\B": (AT, AT_NON_BOUNDARY),
+ r"\d": (IN, [(CATEGORY, CATEGORY_DIGIT)]),
+ r"\D": (IN, [(CATEGORY, CATEGORY_NOT_DIGIT)])