summaryrefslogtreecommitdiff
path: root/libgo/runtime/go-rune.c
blob: 7e31eb8d6229a371436a9aaffd3afb7e3b22ddf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* go-rune.c -- rune functions for Go.

   Copyright 2009, 2010 The Go Authors. All rights reserved.
   Use of this source code is governed by a BSD-style
   license that can be found in the LICENSE file.  */

#include <stddef.h>

#include "go-string.h"

/* Get a character from the UTF-8 string STR, of length LEN.  Store
   the Unicode character, if any, in *RUNE.  Return the number of
   characters used from STR.  */

int
__go_get_rune (const unsigned char *str, size_t len, int *rune)
{
  int c, c1, c2, c3;

  /* Default to the "replacement character".  */
  *rune = 0xfffd;

  if (len <= 0)
    return 1;

  c = *str;
  if (c <= 0x7f)
    {
      *rune = c;
      return 1;
    }

  if (len <= 1)
    return 1;

  c1 = str[1];
  if ((c & 0xe0) == 0xc0
      && (c1 & 0xc0) == 0x80)
    {
      *rune = (((c & 0x1f) << 6)
	       + (c1 & 0x3f));
      return 2;
    }

  if (len <= 2)
    return 1;

  c2 = str[2];
  if ((c & 0xf0) == 0xe0
      && (c1 & 0xc0) == 0x80
      && (c2 & 0xc0) == 0x80)
    {
      *rune = (((c & 0xf) << 12)
	       + ((c1 & 0x3f) << 6)
	       + (c2 & 0x3f));
      return 3;
    }

  if (len <= 3)
    return 1;

  c3 = str[3];
  if ((c & 0xf8) == 0xf0
      && (c1 & 0xc0) == 0x80
      && (c2 & 0xc0) == 0x80
      && (c3 & 0xc0) == 0x80)
    {
      *rune = (((c & 0x7) << 18)
	       + ((c1 & 0x3f) << 12)
	       + ((c2 & 0x3f) << 6)
	       + (c3 & 0x3f));
      return 4;
    }

  /* Invalid encoding.  Return 1 so that we advance.  */
  return 1;
}