From 6c726f3c2fca1d70bfe4d456d0f655013e1a3232 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lebleu?= <jerome.lebleu@mailoo.org>
Date: Sun, 4 Jan 2015 14:27:04 +0100
Subject: [PATCH] [enh] Add text utils with searching helpers

---
 moulinette/utils/text.py | 50 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100644 moulinette/utils/text.py

diff --git a/moulinette/utils/text.py b/moulinette/utils/text.py
new file mode 100644
index 00000000..e7eb06c5
--- /dev/null
+++ b/moulinette/utils/text.py
@@ -0,0 +1,50 @@
+import re
+import mmap
+
+
+# Searching helpers ----------------------------------------------------
+
+def search(pattern, text, count=0, flags=0):
+    """Search for pattern in a text
+
+    Scan through text looking for all locations where the regular
+    expresion pattern matches, and return them as a list of strings.
+
+    The optional argument count is the maximum number of pattern
+    occurences to return; count must be an integer. If omitted or zero,
+    all occurences will be returned. If it's a negative number, occurences
+    to return will be counted backward. If only one occurence is
+    requested, it will be returned as a string.
+
+    The expression's behaviour can be modified by specifying a flags value.
+    Refer to the re module documentation for available variables.
+
+    """
+    match = re.findall(pattern, text, flags)
+    if not match:
+        return None
+    if not count:
+        return match
+
+    # Limit result depending on count value
+    limit = min(len(match), abs(count))
+    if count < 0:
+        match = match[-limit:]
+    else:
+        match = match[:limit]
+    if abs(count) == 1:
+        return match[0]
+    return match
+
+def searchf(pattern, path, count=0, flags=re.MULTILINE):
+    """Search for pattern in a file
+
+    Map the file with given path to memory and search for pattern in it
+    content by using the search function.
+
+    """
+    with open(path, 'r+') as f:
+        data = mmap.mmap(f.fileno(), 0)
+        match = search(pattern, data, count, flags)
+        data.close()
+    return match