eric6/Utilities/__init__.py

changeset 7775
4a1db75550bd
parent 7729
f2f7b7b24879
child 7785
9978016560ec
diff -r 9eed155411f0 -r 4a1db75550bd eric6/Utilities/__init__.py
--- a/eric6/Utilities/__init__.py	Sat Oct 10 16:03:53 2020 +0200
+++ b/eric6/Utilities/__init__.py	Sun Oct 11 17:54:52 2020 +0200
@@ -7,7 +7,6 @@
 Package implementing various functions/classes needed everywhere within eric6.
 """
 
-
 import os
 import sys
 import codecs
@@ -43,8 +42,8 @@
 from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF32
 
 from PyQt5.QtCore import (
-    QRegExp, QDir, QProcess, Qt, QByteArray, qVersion, PYQT_VERSION_STR,
-    QCoreApplication, QCryptographicHash
+    qVersion, PYQT_VERSION_STR, QDir, QProcess, QByteArray, QCoreApplication,
+    QCryptographicHash
 )
 from PyQt5.Qsci import QSCINTILLA_VERSION_STR, QsciScintilla
 
@@ -1365,7 +1364,7 @@
     @param s option string (string or string)
     @return list of options (list of strings)
     """
-    rx = QRegExp(r"""\s([\w=/-]*"[^"]+"|[\w=/-]*'[^']+'|[^\s]+)""")
+    rx = re.compile(r"""\s([\w=/-]*"[^"]+"|[\w=/-]*'[^']+'|[^\s]+)""")
     s = re.sub(r"%[A-Z%]", _percentReplacementFunc, s)
     return parseString(s, rx)
     
@@ -1378,7 +1377,7 @@
     @param s environment string (string)
     @return list of environment settings (list of strings)
     """
-    rx = QRegExp(r"""\s(\w+\+?=[^\s]+|\w+="[^"]+"|\w+='[^']+')""")
+    rx = re.compile(r"""\s(\w+\+?=[^\s]+|\w+="[^"]+"|\w+='[^']+')""")
     return parseString(s, rx)
 
 
@@ -1386,24 +1385,25 @@
     """
     Function used to convert a string into a list.
     
-    @param s string to be parsed (string)
-    @param rx regex defining the parse pattern (QRegExp)
-    @return list of parsed data (list of strings)
+    @param s string to be parsed
+    @type str
+    @param rx regular expression object defining the parse pattern
+    @type re.Pattern
+    @return list of parsed data
+    @rtype list of str
     """
     olist = []
-    if not s.startswith(' '):
-        # prepare the  string to fit our pattern
-        s = ' ' + s
+    if s:
+        if not s.startswith(' '):
+            # prepare the  string to fit our pattern
+            s = ' ' + s
         
-    pos = rx.indexIn(s)
-    while pos != -1:
-        cs = rx.cap(1)
-        if cs.startswith('"') or cs.startswith("'"):
-            cs = cs[1:-1]
-        olist.append(cs)
-        pos += rx.matchedLength()
-        pos = rx.indexIn(s, pos)
-        
+        for match in rx.finditer(s):
+            cs = match.group(1)
+            if cs.startswith('"') or cs.startswith("'"):
+                cs = cs[1:-1]
+            olist.append(cs)
+    
     return olist
 
 
@@ -1662,6 +1662,25 @@
     return pyVer
 
 
+def rxIndex(rx, txt):
+    """
+    Function to get the index (start position) of a regular expression match
+    within some text.
+    
+    @param rx regular expression object as created by re.compile()
+    @type re.Pattern
+    @param txt text to be scanned
+    @type str
+    @return start position of the match or -1 indicating no match was found
+    @rtype int
+    """
+    match = rx.search(txt)
+    if match is None:
+        return -1
+    else:
+        return match.start()
+
+
 ###############################################################################
 # functions for environment handling
 ###############################################################################
@@ -1677,33 +1696,39 @@
     @return the requested entry or the default value, if the entry wasn't
         found (string or None)
     """
-    filterRe = QRegExp("^{0}[ \t]*=".format(key))
+    pattern = "^{0}[ \t]*=".format(key)
     if isWindowsPlatform():
-        filterRe.setCaseSensitivity(Qt.CaseInsensitive)
+        filterRe = re.compile(pattern, re.IGNORECASE)
+    else:
+        filterRe = re.compile(pattern)
     
     entries = [e for e in QProcess.systemEnvironment()
-               if filterRe.indexIn(e) != -1]
+               if filterRe.search(e) is not None]
     if not entries:
         return default
     
     # if there are multiple entries, just consider the first one
-    ename, val = entries[0].split("=", 1)
-    return val.strip()
+    ename, value = entries[0].split("=", 1)
+    return value.strip()
 
 
 def hasEnvironmentEntry(key):
     """
     Module function to check, if the environment contains an entry.
     
-    @param key key of the requested environment entry (string)
-    @return flag indicating the presence of the requested entry (boolean)
+    @param key key of the requested environment entry
+    @type str
+    @return flag indicating the presence of the requested entry
+    @rtype bool
     """
-    filterRe = QRegExp("^{0}[ \t]*=".format(key))
+    pattern = "^{0}[ \t]*=".format(key)
     if isWindowsPlatform():
-        filterRe.setCaseSensitivity(Qt.CaseInsensitive)
+        filterRe = re.compile(pattern, re.IGNORECASE)
+    else:
+        filterRe = re.compile(pattern)
     
     entries = [e for e in QProcess.systemEnvironment()
-               if filterRe.indexIn(e) != -1]
+               if filterRe.search(e) is not None]
     return len(entries) > 0
 
 ###############################################################################

eric ide

mercurial