OSDN Git Service

mn10300: Add attribute enabled.
[pf3gnuchains/gcc-fork.git] / libgo / runtime / go-int-to-string.c
1 /* go-int-to-string.c -- convert an integer to a string in Go.
2
3    Copyright 2009 The Go Authors. All rights reserved.
4    Use of this source code is governed by a BSD-style
5    license that can be found in the LICENSE file.  */
6
7 #include "go-string.h"
8 #include "runtime.h"
9 #include "malloc.h"
10
11 struct __go_string
12 __go_int_to_string (int v)
13 {
14   char buf[4];
15   int len;
16   unsigned char *retdata;
17   struct __go_string ret;
18
19   if (v <= 0x7f)
20     {
21       buf[0] = v;
22       len = 1;
23     }
24   else if (v <= 0x7ff)
25     {
26       buf[0] = 0xc0 + (v >> 6);
27       buf[1] = 0x80 + (v & 0x3f);
28       len = 2;
29     }
30   else
31     {
32       /* If the value is out of range for UTF-8, turn it into the
33          "replacement character".  */
34       if (v > 0x10ffff)
35         v = 0xfffd;
36
37       if (v <= 0xffff)
38         {
39           buf[0] = 0xe0 + (v >> 12);
40           buf[1] = 0x80 + ((v >> 6) & 0x3f);
41           buf[2] = 0x80 + (v & 0x3f);
42           len = 3;
43         }
44       else
45         {
46           buf[0] = 0xf0 + (v >> 18);
47           buf[1] = 0x80 + ((v >> 12) & 0x3f);
48           buf[2] = 0x80 + ((v >> 6) & 0x3f);
49           buf[3] = 0x80 + (v & 0x3f);
50           len = 4;
51         }
52     }
53
54   retdata = runtime_mallocgc (len, RefNoPointers, 1, 0);
55   __builtin_memcpy (retdata, buf, len);
56   ret.__data = retdata;
57   ret.__length = len;
58
59   return ret;
60 }