Converts a string with possible high-ascii values into their ascii7 equiv., for instance 'é'
maps to 'e'. All low ascii characters are left untouched. Any character above 255 is converted
to a space.
427411
public static String getUS7ASCIIEquiv(String convertString) {
    if (convertString == null)
      return null;

    StringBuilder convertedString = new StringBuilder();
    String upperConvertString = convertString.toUpperCase();

    Collator collator = Collator.getInstance(Locale.US);
    collator.setStrength(Collator.PRIMARY);

    String chars = "abcdefghijklmnopqrstuvwxyz";
    for (int i = 0; i < convertString.length(); i++) {
      char currentChar = convertString.charAt(i);
      boolean isUppercase = currentChar == upperConvertString.charAt(i);
      char mappedChar = ' ';
      if (currentChar < 128) {
        // Within low-ascii, leave it alone.
        mappedChar = currentChar;
      } else if (currentChar < 256) {
        for (int j = 0; j < chars.length(); j++) {
          if (collator.compare(currentChar + "", chars.charAt(j) + "") == 0) {
            if (isUppercase)
              mappedChar = Character.toUpperCase(chars.charAt(j));
            else
              mappedChar = chars.charAt(j);
            break;
          }
        }
      } else {
        // Out of our mapping range. It just becomes a space
      }
      convertedString.append(mappedChar);
    }
    return convertedString.toString();
  }
1079274
def encode_string_with_trailing_spaces(string, target_length)
      length = string.length
      if length < target_length
        return "#{[string].pack(@str)}#{['20'*(target_length-length)].pack(@hex)}"
      elsif length == target_length
        return [string].pack(@str)
      else
        raise "The specified string is longer than the allowed maximum length (String: #{string}, Target length: #{target_length})."
      end
    end
685497
def convert_to_utf8(string):
    '''  '''
    if (isinstance(string, unicode)):
        return string.encode('utf-8')
    try:
        u = unicode(string, 'utf-8')
    except TypeError:
        return str(string)
    utf8 = u.encode('utf-8')
    return utf8
69705
def _EncodeString(self, string):
    
    try:
      # Note that encode() will first convert string into a Unicode string
      # if necessary.
      encoded_string = string.encode(
          self.preferred_encoding, errors=self._encode_errors)
    except UnicodeEncodeError:
      if self._encode_errors == 'strict':
        logger.error(
            'Unable to properly write output due to encoding error. '
            'Switching to error tolerant encoding which can result in '
            'non Basic Latin (C0) characters to be replaced with "?" or '
            '"\\ufffd".')
        self._encode_errors = 'replace'

      encoded_string = string.encode(
          self.preferred_encoding, errors=self._encode_errors)

    return encoded_string
429796
function (string) {
			var ascii = "";

			for (var i = 0; i < string.length; i++) {
				ascii += string.charCodeAt(i) + ",";
			}

			// remove last ","
			ascii = ascii.substring(0, ascii.length - 1);

			return ascii;
		}
1105526
def ascii(text):
    
    if not isinstance(text, unicode):
        text = unicode(text)
    return unicodedata.normalize('NFKD', text).encode('ascii', 'ignore')
556621
def encode_non_ascii_string(string):
    
    encoded_string = string.encode('utf-8', 'replace')
    if six.PY3:
        encoded_string = encoded_string.decode()

    return encoded_string
287327
function utf8ByteLength(str) {
  let s = str.length;
  let i;
  for (i = str.length - 1; i >= 0; i -= 1) {
    const code = str.charCodeAt(i);
    if (code > 0x7f && code <= 0x7ff) {
      s += 1;
    } else if (code > 0x7ff && code <= 0xffff) {
      s += 2;
    }

    // trail surrogate
    if (code >= 0xDC00 && code <= 0xDFFF) {
      i -= 1;
    }
  }

  return s;
}
1181303
function decode (str) {
  return str.trim().split(' ').map((s) => {
    if (s.charCodeAt(0) - 0x2800 > 127) return s
    return String.fromCharCode(s.charCodeAt(0) - 0x2800)
  }).join('')
}
1045305
def encode(strs):
    
    res = ''
    for string in strs.split():
        res += str(len(string)) + ":" + string
    return res
954839
def utf8_encode_str(string, encoding='UTF-8'):
    
    if not string:
        return ''
    src_enc = chardet.detect(string)['encoding']
    try:
        return string.decode(src_enc).encode(encoding)
    except:
        return string.decode('ascii', errors='replace').encode(encoding)
202044
def unicode_is_ascii(u_string):
    
    assert isinstance(u_string, str)
    try:
        u_string.encode('ascii')
        return True
    except UnicodeEncodeError:
        return False
309217
def normalize_str(string):
    
    if string:
        if not isinstance(string, str):
            string = str(string, 'utf-8', 'replace')

        string = string.encode('utf-8')
        return normalize(
            'NFKD', string.decode('utf-8')).encode('ASCII', 'ignore').decode()
    return ''
385672
def normalize(string: Text) -> Text:
    

    string = string.lower()
    string = unidecode(string)
    string = RE_WHITESPACES.sub(' ', string).strip()

    return string
486475
def convert(self, string, preprocess = None):
        
        string = unicode(preprocess(string) if preprocess else string, encoding="utf-8")
        if self.regex:
            return self.regex.sub(lambda x: self.substitutes[x.group()], string).encode('utf-8')
        else:
            return string
1278134
function encode(str) {
  var result = '',
      bytes = utf8Encode(str),
      length = bytes.length,
      i;

  // Convert every three bytes to 4 ascii characters.
  for (i = 0; i < (length - 2); i += 3) {
    result += chrTable[bytes[i] >> 2];
    result += chrTable[((bytes[i] & 0x03) << 4) + (bytes[i + 1] >> 4)];
    result += chrTable[((bytes[i + 1] & 0x0f) << 2) + (bytes[i + 2] >> 6)];
    result += chrTable[bytes[i + 2] & 0x3f];
  }

  // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
  if (length % 3) {
    i = length - (length % 3);
    result += chrTable[bytes[i] >> 2];
    if ((length % 3) === 2) {
      result += chrTable[((bytes[i] & 0x03) << 4) + (bytes[i + 1] >> 4)];
      result += chrTable[(bytes[i + 1] & 0x0f) << 2];
      result += padding;
    } else {
      result += chrTable[(bytes[i] & 0x03) << 4];
      result += padding + padding;
    }
  }

  return result;
}
807559
def to_safe_str(s):
    
    # TODO: This is insufficient as it doesn't do anything for other non-ascii chars
    return re.sub(r'[^0-9a-zA-Z]+', '_', s.strip().replace(u'ğ', 'g').replace(u'ö', 'o').replace(
        u'ç', 'c').replace(u'Ç','c').replace(u'Ö', u'O').replace(u'Ş', 's').replace(
        u'Ü', 'u').replace(u'ı', 'i').replace(u'İ','i').replace(u'Ğ', 'g').replace(
        u'ö', 'o').replace(u'ş', 's').replace(u'ü', 'u').lower(), re.UNICODE)