added is_ascii and indent

git-svn-id: https://linkchecker.svn.sourceforge.net/svnroot/linkchecker/trunk/linkchecker@2806 e7d03fd6-7b0d-0410-9947-9c21f3af8025
This commit is contained in:
calvin 2005-08-16 13:55:51 +00:00
parent 866999a883
commit 4b697eccea

View file

@ -60,6 +60,17 @@ def ascii_safe (s):
return s
def is_ascii (s):
"""
Test if a string can be encoded in ASCII.
"""
try:
s.encode('ascii', 'strict')
return True
except (UnicodeEncodeError, UnicodeDecodeError):
return False
def url_unicode_split (url):
"""
Like urlparse.urlsplit(), but always returning unicode parts.
@ -123,6 +134,14 @@ def wrap (text, width, **kwargs):
return os.linesep.join(ret)
def indent (text, indent_string=" "):
"""
Indent each line of text with the given indent string.
"""
lines = str(text).splitlines()
return os.linesep.join(["%s%s" % (indent_string, x) for x in lines])
def get_line_number (s, index):
"""
Return the line number of s[index]. Lines are assumed to be separated