Source file src/runtime/utf8.go

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package runtime
     6  
     7  // Numbers fundamental to the encoding.
     8  const (
     9  	runeError = '\uFFFD'     // the "error" Rune or "Unicode replacement character"
    10  	runeSelf  = 0x80         // characters below runeSelf are represented as themselves in a single byte.
    11  	maxRune   = '\U0010FFFF' // Maximum valid Unicode code point.
    12  )
    13  
    14  // Code points in the surrogate range are not valid for UTF-8.
    15  const (
    16  	surrogateMin = 0xD800
    17  	surrogateMax = 0xDFFF
    18  )
    19  
    20  const (
    21  	t1 = 0x00 // 0000 0000
    22  	tx = 0x80 // 1000 0000
    23  	t2 = 0xC0 // 1100 0000
    24  	t3 = 0xE0 // 1110 0000
    25  	t4 = 0xF0 // 1111 0000
    26  	t5 = 0xF8 // 1111 1000
    27  
    28  	maskx = 0x3F // 0011 1111
    29  	mask2 = 0x1F // 0001 1111
    30  	mask3 = 0x0F // 0000 1111
    31  	mask4 = 0x07 // 0000 0111
    32  
    33  	rune1Max = 1<<7 - 1
    34  	rune2Max = 1<<11 - 1
    35  	rune3Max = 1<<16 - 1
    36  
    37  	// The default lowest and highest continuation byte.
    38  	locb = 0x80 // 1000 0000
    39  	hicb = 0xBF // 1011 1111
    40  )
    41  
    42  // countrunes returns the number of runes in s.
    43  func countrunes(s string) int {
    44  	n := 0
    45  	for range s {
    46  		n++
    47  	}
    48  	return n
    49  }
    50  
    51  // decoderune returns the non-ASCII rune at the start of
    52  // s[k:] and the index after the rune in s.
    53  //
    54  // decoderune assumes that caller has checked that
    55  // the to be decoded rune is a non-ASCII rune.
    56  //
    57  // If the string appears to be incomplete or decoding problems
    58  // are encountered (runeerror, k + 1) is returned to ensure
    59  // progress when decoderune is used to iterate over a string.
    60  func decoderune(s string, k int) (r rune, pos int) {
    61  	pos = k
    62  
    63  	if k >= len(s) {
    64  		return runeError, k + 1
    65  	}
    66  
    67  	s = s[k:]
    68  
    69  	switch {
    70  	case t2 <= s[0] && s[0] < t3:
    71  		// 0080-07FF two byte sequence
    72  		if len(s) > 1 && (locb <= s[1] && s[1] <= hicb) {
    73  			r = rune(s[0]&mask2)<<6 | rune(s[1]&maskx)
    74  			pos += 2
    75  			if rune1Max < r {
    76  				return
    77  			}
    78  		}
    79  	case t3 <= s[0] && s[0] < t4:
    80  		// 0800-FFFF three byte sequence
    81  		if len(s) > 2 && (locb <= s[1] && s[1] <= hicb) && (locb <= s[2] && s[2] <= hicb) {
    82  			r = rune(s[0]&mask3)<<12 | rune(s[1]&maskx)<<6 | rune(s[2]&maskx)
    83  			pos += 3
    84  			if rune2Max < r && !(surrogateMin <= r && r <= surrogateMax) {
    85  				return
    86  			}
    87  		}
    88  	case t4 <= s[0] && s[0] < t5:
    89  		// 10000-1FFFFF four byte sequence
    90  		if len(s) > 3 && (locb <= s[1] && s[1] <= hicb) && (locb <= s[2] && s[2] <= hicb) && (locb <= s[3] && s[3] <= hicb) {
    91  			r = rune(s[0]&mask4)<<18 | rune(s[1]&maskx)<<12 | rune(s[2]&maskx)<<6 | rune(s[3]&maskx)
    92  			pos += 4
    93  			if rune3Max < r && r <= maxRune {
    94  				return
    95  			}
    96  		}
    97  	}
    98  
    99  	return runeError, k + 1
   100  }
   101  
   102  // encoderune writes into p (which must be large enough) the UTF-8 encoding of the rune.
   103  // It returns the number of bytes written.
   104  func encoderune(p []byte, r rune) int {
   105  	// Negative values are erroneous. Making it unsigned addresses the problem.
   106  	switch i := uint32(r); {
   107  	case i <= rune1Max:
   108  		p[0] = byte(r)
   109  		return 1
   110  	case i <= rune2Max:
   111  		_ = p[1] // eliminate bounds checks
   112  		p[0] = t2 | byte(r>>6)
   113  		p[1] = tx | byte(r)&maskx
   114  		return 2
   115  	case i > maxRune, surrogateMin <= i && i <= surrogateMax:
   116  		r = runeError
   117  		fallthrough
   118  	case i <= rune3Max:
   119  		_ = p[2] // eliminate bounds checks
   120  		p[0] = t3 | byte(r>>12)
   121  		p[1] = tx | byte(r>>6)&maskx
   122  		p[2] = tx | byte(r)&maskx
   123  		return 3
   124  	default:
   125  		_ = p[3] // eliminate bounds checks
   126  		p[0] = t4 | byte(r>>18)
   127  		p[1] = tx | byte(r>>12)&maskx
   128  		p[2] = tx | byte(r>>6)&maskx
   129  		p[3] = tx | byte(r)&maskx
   130  		return 4
   131  	}
   132  }
   133  

View as plain text