new methods stripall and limit

git-svn-id: https://linkchecker.svn.sourceforge.net/svnroot/linkchecker/trunk/linkchecker@2818 e7d03fd6-7b0d-0410-9947-9c21f3af8025
This commit is contained in:
calvin 2005-10-10 21:14:05 +00:00
parent b28be779d7
commit e11bc21593
2 changed files with 40 additions and 0 deletions

View file

@ -230,3 +230,32 @@ def strtimezone ():
else:
zone = time.timezone
return "%+04d" % int(-zone/3600)
_sub_ws = re.compile(r"\s+").sub
def stripall (s):
"""
Remove all whitespace from given string.
"""
if not s:
return s
return _sub_ws("", s)
def limit (s, length=72):
"""
If the length of the string exceeds the given limit, it will be cut
off and three dots will be appended.
@param s: the string to limit
@type s: string
@param length: maximum length
@type length: non-negative integer
@return: limited string, at most length+3 characters long
"""
assert length >= 0, "length limit must be a non-negative integer"
if not s or len(s) <= length:
return s
if length == 0:
return ""
return "%s..." % s[:length]

View file

@ -99,6 +99,17 @@ class TestStrFormat (unittest.TestCase):
self.assertEquals(linkcheck.strformat.indent(s, ""), s)
self.assertEquals(linkcheck.strformat.indent(s, " "), " "+s)
def test_stripall (self):
self.assertEquals(linkcheck.strformat.stripall("a\tb"), "ab")
self.assertEquals(linkcheck.strformat.stripall(" a\t b"), "ab")
self.assertEquals(linkcheck.strformat.stripall(" \r\na\t \nb\r"), "ab")
def test_limit (self):
self.assertEquals(linkcheck.strformat.limit("", 0), "")
self.assertEquals(linkcheck.strformat.limit("a", 0), "")
self.assertEquals(linkcheck.strformat.limit("1", 1), "1")
self.assertEquals(linkcheck.strformat.limit("11", 1), "1...")
def test_suite ():
"""