putString method

void putString(
  1. int fieldId,
  2. String value
)

Writes a UTF-8 string field.

Encodes straight into the output buffer in a single pass and assumes a one-byte length prefix (any string under 128 bytes, i.e. almost all of them). Wider strings fall back to the allocating encoder.

Implementation

void putString(int fieldId, String value) {
  _beginEntry(fieldId, VxsWire.lengthDelimited);
  final units = value.length;
  if (units == 0) {
    _writeVarint(0);
    return;
  }
  final start = _pos;
  _ensure(1 + units * 3);
  final written = encodeUtf8(value, _data, start + 1);
  if (written < 0x80) {
    _data[start] = written;
    _pos = start + 1 + written;
    return;
  }
  final bytes = _utf8Fallback(value);
  _writeVarint(bytes.length);
  _ensure(bytes.length);
  _data.setRange(_pos, _pos + bytes.length, bytes);
  _pos += bytes.length;
}