Importing Upstream version 4.8.2
[platform/upstream/gcc48.git] / libgo / runtime / string.goc
1 // Copyright 2009, 2010 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 #include "runtime.h"
7 #include "arch.h"
8 #include "malloc.h"
9 #include "go-string.h"
10 #include "race.h"
11
12 #define charntorune(pv, str, len) __go_get_rune(str, len, pv)
13
14 const String    runtime_emptystring;
15
16 intgo
17 runtime_findnull(const byte *s)
18 {
19         if(s == nil)
20                 return 0;
21         return __builtin_strlen((const char*) s);
22 }
23
24 static String
25 gostringsize(intgo l, byte** pmem)
26 {
27         String s;
28         byte *mem;
29
30         if(l == 0) {
31                 *pmem = nil;
32                 return runtime_emptystring;
33         }
34         // leave room for NUL for C runtime (e.g., callers of getenv)
35         mem = runtime_mallocgc(l+1, FlagNoPointers, 1, 0);
36         s.str = mem;
37         s.len = l;
38         mem[l] = 0;
39         *pmem = mem;
40         return s;
41 }
42
43 String
44 runtime_gostring(const byte *str)
45 {
46         intgo l;
47         String s;
48         byte *mem;
49
50         l = runtime_findnull(str);
51         s = gostringsize(l, &mem);
52         runtime_memmove(mem, str, l);
53         return s;
54 }
55
56 String
57 runtime_gostringnocopy(const byte *str)
58 {
59         String s;
60         
61         s.str = str;
62         s.len = runtime_findnull(str);
63         return s;
64 }
65
66 enum
67 {
68         Runeself        = 0x80,
69 };
70
71 func stringiter(s String, k int) (retk int) {
72         int32 l;
73
74         if(k >= s.len) {
75                 // retk=0 is end of iteration
76                 retk = 0;
77                 goto out;
78         }
79
80         l = s.str[k];
81         if(l < Runeself) {
82                 retk = k+1;
83                 goto out;
84         }
85
86         // multi-char rune
87         retk = k + charntorune(&l, s.str+k, s.len-k);
88
89 out:
90 }
91
92 func stringiter2(s String, k int) (retk int, retv int32) {
93         if(k >= s.len) {
94                 // retk=0 is end of iteration
95                 retk = 0;
96                 retv = 0;
97                 goto out;
98         }
99
100         retv = s.str[k];
101         if(retv < Runeself) {
102                 retk = k+1;
103                 goto out;
104         }
105
106         // multi-char rune
107         retk = k + charntorune(&retv, s.str+k, s.len-k);
108
109 out:
110 }