charset property Null safety

String charset

The charset parameter of the media type.

If the parameters of the media type contains a charset parameter then this returns its value, otherwise it returns US-ASCII, which is the default charset for data URIs. If the value contain non-ASCII percent escapes, they are decoded as UTF-8.

If the MIME type representation in the URI text contains URI escapes, they are unescaped in the returned string.

Implementation

String get charset {
  int parameterStart = 1;
  int parameterEnd = _separatorIndices.length - 1; // The ',' before data.
  if (isBase64) {
    // There is a ";base64" separator, so subtract one for that as well.
    parameterEnd -= 1;
  }
  for (int i = parameterStart; i < parameterEnd; i += 2) {
    var keyStart = _separatorIndices[i] + 1;
    var keyEnd = _separatorIndices[i + 1];
    if (keyEnd == keyStart + 7 && _text.startsWith("charset", keyStart)) {
      return _Uri._uriDecode(
          _text, keyEnd + 1, _separatorIndices[i + 2], utf8, false);
    }
  }
  return "US-ASCII";
}