aboutsummaryrefslogtreecommitdiffstats
path: root/netlib
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2016-06-02 13:03:37 +1200
committerAldo Cortesi <aldo@nullcube.com>2016-06-02 13:04:19 +1200
commit09da1febbd9beac5ef5650274899439f5ce10e98 (patch)
tree76ba04357353645f49e0f4bd8d835e471239b54b /netlib
parent31012d782f64727de1d86662139e9ec6618043c5 (diff)
downloadmitmproxy-09da1febbd9beac5ef5650274899439f5ce10e98.tar.gz
mitmproxy-09da1febbd9beac5ef5650274899439f5ce10e98.tar.bz2
mitmproxy-09da1febbd9beac5ef5650274899439f5ce10e98.zip
Shift a bunch more string-related functions to strutils
Diffstat (limited to 'netlib')
-rw-r--r--netlib/strutils.py51
-rw-r--r--netlib/utils.py15
2 files changed, 51 insertions, 15 deletions
diff --git a/netlib/strutils.py b/netlib/strutils.py
index 7a62185b..03b371f5 100644
--- a/netlib/strutils.py
+++ b/netlib/strutils.py
@@ -101,3 +101,54 @@ def escaped_str_to_bytes(data):
# This one is difficult - we use an undocumented Python API here
# as per http://stackoverflow.com/a/23151714/934719
return codecs.escape_decode(data)[0]
+
+
+def isBin(s):
+ """
+ Does this string have any non-ASCII characters?
+ """
+ for i in s:
+ i = ord(i)
+ if i < 9 or 13 < i < 32 or 126 < i:
+ return True
+ return False
+
+
+def isMostlyBin(s):
+ s = s[:100]
+ return sum(isBin(ch) for ch in s) / len(s) > 0.3
+
+
+def isXML(s):
+ for i in s:
+ if i in "\n \t":
+ continue
+ elif i == "<":
+ return True
+ else:
+ return False
+
+
+def clean_hanging_newline(t):
+ """
+ Many editors will silently add a newline to the final line of a
+ document (I'm looking at you, Vim). This function fixes this common
+ problem at the risk of removing a hanging newline in the rare cases
+ where the user actually intends it.
+ """
+ if t and t[-1] == "\n":
+ return t[:-1]
+ return t
+
+
+def hexdump(s):
+ """
+ Returns:
+ A generator of (offset, hex, str) tuples
+ """
+ for i in range(0, len(s), 16):
+ offset = "{:0=10x}".format(i).encode()
+ part = s[i:i + 16]
+ x = b" ".join("{:0=2x}".format(i).encode() for i in six.iterbytes(part))
+ x = x.ljust(47) # 16*2 + 15
+ yield (offset, x, clean_bin(part, False))
diff --git a/netlib/utils.py b/netlib/utils.py
index 00e7e5d9..b4b99679 100644
--- a/netlib/utils.py
+++ b/netlib/utils.py
@@ -6,21 +6,6 @@ import inspect
import six
-from netlib import strutils
-
-
-def hexdump(s):
- """
- Returns:
- A generator of (offset, hex, str) tuples
- """
- for i in range(0, len(s), 16):
- offset = "{:0=10x}".format(i).encode()
- part = s[i:i + 16]
- x = b" ".join("{:0=2x}".format(i).encode() for i in six.iterbytes(part))
- x = x.ljust(47) # 16*2 + 15
- yield (offset, x, strutils.clean_bin(part, False))
-
def setbit(byte, offset, value):
"""