# Path: tornado/escape.py
# def utf8(value):
#     """Converts a string argument to a byte string.
# 
#     If the argument is already a byte string or None, it is returned unchanged.
#     Otherwise it must be a unicode string and is encoded as utf8.
#     """
#     if isinstance(value, _UTF8_TYPES):
#         return value
#     assert isinstance(value, unicode)
#     return value.encode("utf-8")
# 
# def xhtml_escape(value):
#     """Escapes a string so it is valid within XML or XHTML."""
#     return _XHTML_ESCAPE_RE.sub(lambda match: _XHTML_ESCAPE_DICT[match.group(0)],
#                                 to_basestring(value))
# 
# def xhtml_unescape(value):
#     """Un-escapes an XML-escaped string."""
#     return re.sub(r"&(#?)(\w+?);", _convert_entity, _unicode(value))
# 
# def url_escape(value):
#     """Returns a valid URL-encoded version of the given value."""
#     return urllib.quote_plus(utf8(value))
# 
# def url_unescape(value, encoding='utf-8'):
#     """Decodes the given value from a URL.
# 
#     The argument may be either a byte or unicode string.
# 
#     If encoding is None, the result will be a byte string.  Otherwise,
#     the result is a unicode string in the specified encoding.
#     """
#     if encoding is None:
#         return urllib.unquote_plus(utf8(value))
#     else:
#         return unicode(urllib.unquote_plus(utf8(value)), encoding)
# 
# def to_unicode(value):
#     """Converts a string argument to a unicode string.
# 
#     If the argument is already a unicode string or None, it is returned
#     unchanged.  Otherwise it must be a byte string and is decoded as utf8.
#     """
#     if isinstance(value, _TO_UNICODE_TYPES):
#         return value
#     assert isinstance(value, bytes)
#     return value.decode("utf-8")
# 
# def json_decode(value):
#     """Returns Python objects for the given JSON string."""
#     return _json_decode(to_basestring(value))
# 
# def json_encode(value):
#     """JSON-encodes the given Python object."""
#     # JSON permits but does not require forward slashes to be escaped.
#     # This is useful when json data is emitted in a <script> tag
#     # in HTML, as it prevents </script> tags from prematurely terminating
#     # the javscript.  Some json libraries do this escaping by default,
#     # although python's standard library does not, so we do it here.
#     # http://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped
#     return _json_encode(recursive_unicode(value)).replace("</", "<\\/")
# 
# Path: tornado/util.py
# def b(s):
#     return s.encode('latin1')


import tornado.escape
import unittest
from tornado.escape import utf8, xhtml_escape, xhtml_unescape, url_escape, url_unescape, to_unicode, json_decode, json_encode
from tornado.util import b
     u'<a href="http://foo.com/blah_blah_(wikipedia)">http://foo.com/blah_blah_(wikipedia)</a>'),

    ("http://foo.com/blah_(blah)_(wikipedia)_blah", {},
     u'<a href="http://foo.com/blah_(blah)_(wikipedia)_blah">http://foo.com/blah_(blah)_(wikipedia)_blah</a>'),

    ("(Something like http://foo.com/blah_blah_(wikipedia))", {},
     u'(Something like <a href="http://foo.com/blah_blah_(wikipedia)">http://foo.com/blah_blah_(wikipedia)</a>)'),

    ("http://foo.com/blah_blah.", {},
     u'<a href="http://foo.com/blah_blah">http://foo.com/blah_blah</a>.'),

    ("http://foo.com/blah_blah/.", {},
     u'<a href="http://foo.com/blah_blah/">http://foo.com/blah_blah/</a>.'),

    ("<http://foo.com/blah_blah>", {},
     u'&lt;<a href="http://foo.com/blah_blah">http://foo.com/blah_blah</a>&gt;'),

    ("<http://foo.com/blah_blah/>", {},
     u'&lt;<a href="http://foo.com/blah_blah/">http://foo.com/blah_blah/</a>&gt;'),

    ("http://foo.com/blah_blah,", {},
     u'<a href="http://foo.com/blah_blah">http://foo.com/blah_blah</a>,'),

    ("http://www.example.com/wpstyle/?p=364.", {},
     u'<a href="http://www.example.com/wpstyle/?p=364">http://www.example.com/wpstyle/?p=364</a>.'),

    ("rdar://1234", 
     {"permitted_protocols": ["http", "rdar"]},
     u'<a href="rdar://1234">rdar://1234</a>'),


# Path: tornado/escape.py
# def utf8(value):
#     """Converts a string argument to a byte string.
# 
#     If the argument is already a byte string or None, it is returned unchanged.
#     Otherwise it must be a unicode string and is encoded as utf8.
#     """
#     if isinstance(value, _UTF8_TYPES):
#         return value
#     assert isinstance(value, unicode)
#     return value.encode("utf-8")
# 
# def xhtml_escape(value):
#     """Escapes a string so it is valid within XML or XHTML."""
#     return _XHTML_ESCAPE_RE.sub(lambda match: _XHTML_ESCAPE_DICT[match.group(0)],
#                                 to_basestring(value))
# 
# def xhtml_unescape(value):
#     """Un-escapes an XML-escaped string."""
#     return re.sub(r"&(#?)(\w+?);", _convert_entity, _unicode(value))
# 
# def url_escape(value):
#     """Returns a valid URL-encoded version of the given value."""
#     return urllib.quote_plus(utf8(value))
# 
# def url_unescape(value, encoding='utf-8'):
#     """Decodes the given value from a URL.
# 
#     The argument may be either a byte or unicode string.
# 
#     If encoding is None, the result will be a byte string.  Otherwise,
#     the result is a unicode string in the specified encoding.
#     """
#     if encoding is None:
#         return urllib.unquote_plus(utf8(value))
#     else:
#         return unicode(urllib.unquote_plus(utf8(value)), encoding)
# 
# def to_unicode(value):
#     """Converts a string argument to a unicode string.
# 
#     If the argument is already a unicode string or None, it is returned
#     unchanged.  Otherwise it must be a byte string and is decoded as utf8.
#     """
#     if isinstance(value, _TO_UNICODE_TYPES):
#         return value
#     assert isinstance(value, bytes)
#     return value.decode("utf-8")
# 
# def json_decode(value):
#     """Returns Python objects for the given JSON string."""
#     return _json_decode(to_basestring(value))
# 
# def json_encode(value):
#     """JSON-encodes the given Python object."""
#     # JSON permits but does not require forward slashes to be escaped.
#     # This is useful when json data is emitted in a <script> tag
#     # in HTML, as it prevents </script> tags from prematurely terminating
#     # the javscript.  Some json libraries do this escaping by default,
#     # although python's standard library does not, so we do it here.
#     # http://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped
#     return _json_encode(recursive_unicode(value)).replace("</", "<\\/")
# 
# Path: tornado/util.py
# def b(s):
#     return s.encode('latin1')


# Path: tornado/test/escape_test.py
import tornado.escape
import unittest
from tornado.escape import utf8, xhtml_escape, xhtml_unescape, url_escape, url_unescape, to_unicode, json_decode, json_encode
from tornado.util import b

     u'<a href="http://foo.com/blah_blah_(wikipedia)">http://foo.com/blah_blah_(wikipedia)</a>'),

    ("http://foo.com/blah_(blah)_(wikipedia)_blah", {},
     u'<a href="http://foo.com/blah_(blah)_(wikipedia)_blah">http://foo.com/blah_(blah)_(wikipedia)_blah</a>'),

    ("(Something like http://foo.com/blah_blah_(wikipedia))", {},
     u'(Something like <a href="http://foo.com/blah_blah_(wikipedia)">http://foo.com/blah_blah_(wikipedia)</a>)'),

    ("http://foo.com/blah_blah.", {},
     u'<a href="http://foo.com/blah_blah">http://foo.com/blah_blah</a>.'),

    ("http://foo.com/blah_blah/.", {},
     u'<a href="http://foo.com/blah_blah/">http://foo.com/blah_blah/</a>.'),

    ("<http://foo.com/blah_blah>", {},
     u'&lt;<a href="http://foo.com/blah_blah">http://foo.com/blah_blah</a>&gt;'),

    ("<http://foo.com/blah_blah/>", {},
     u'&lt;<a href="http://foo.com/blah_blah/">http://foo.com/blah_blah/</a>&gt;'),

    ("http://foo.com/blah_blah,", {},
     u'<a href="http://foo.com/blah_blah">http://foo.com/blah_blah</a>,'),

    ("http://www.example.com/wpstyle/?p=364.", {},
     u'<a href="http://www.example.com/wpstyle/?p=364">http://www.example.com/wpstyle/?p=364</a>.'),

    ("rdar://1234", 
     {"permitted_protocols": ["http", "rdar"]},
     u'<a href="rdar://1234">rdar://1234</a>'),


    ("rdar:/1234", 