Jorge Leitao
Jorge Leitao

Reputation: 20123

Are byte slices of utf8 also utf8?

Given a slice of bytes that is valid utf8, is it true that any sub-slice of such slice is also valid utf8?

In other words, given b1: [u8] that is valid utf8, can I assume that

b2 = b1[i..j] is valid utf8 for any i,j : i<j?

If not, what would be the counter-example?

Upvotes: 2

Views: 81

Answers (1)

KamilCuk
KamilCuk

Reputation: 141060

what would be the counter-example?

Any code point that encodes as more than 1 byte. For example π in hex is cf80, and slicing it in the middle produces two (separate) invalid UTF-8 strings.

Upvotes: 5

Related Questions