def _encode(self, value, legal, pctEncoded):
        output = ''
        index = 0
        while (index < len(value)):
            codepoint = value[index]
            if (codepoint in legal):
                output += codepoint
            elif (pctEncoded and ('%' == codepoint) and
                  ((index + 2) < len(value)) and
                  (value[index + 1] in URITemplate.hexdigit) and
                  (value[index + 2] in URITemplate.hexdigit)):
                output += value[index:index + 3]
                index += 2
            else:
                utf8 = codepoint.encode('utf8')
                for byte in utf8:
                    output += '%' + URITemplate.hexdigit[ord(byte) / 16] + URITemplate.hexdigit[ord(byte) % 16]
            index += 1
        return output
dataset/ETHPy150Open tabatkins/bikeshed/bikeshed/apiclient/apiclient/uritemplate.py/Expression._encode/original