2016-07-13 Thomas Preud'homme <thomas.preudhomme@arm.com>
[official-gcc.git] / libgo / runtime / string.goc
blob0ad180b9832d70bbac11a47ac461d101322fa653
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.
5 package runtime
6 #include "runtime.h"
7 #include "arch.h"
8 #include "malloc.h"
9 #include "go-string.h"
11 #define charntorune(pv, str, len) __go_get_rune(str, len, pv)
13 const String    runtime_emptystring;
15 intgo
16 runtime_findnull(const byte *s)
18         if(s == nil)
19                 return 0;
20         return __builtin_strlen((const char*) s);
23 intgo
24 runtime_findnullw(const uint16 *s)
26         intgo l;
28         if(s == nil)
29                 return 0;
30         for(l=0; s[l]!=0; l++)
31                 ;
32         return l;
35 static String
36 gostringsize(intgo l, byte** pmem)
38         String s;
39         byte *mem;
41         if(l == 0) {
42                 *pmem = nil;
43                 return runtime_emptystring;
44         }
45         mem = runtime_mallocgc(l, 0, FlagNoScan|FlagNoZero);
46         s.str = mem;
47         s.len = l;
48         *pmem = mem;
49         return s;
52 String
53 runtime_gostring(const byte *str)
55         intgo l;
56         String s;
57         byte *mem;
59         l = runtime_findnull(str);
60         s = gostringsize(l, &mem);
61         runtime_memmove(mem, str, l);
62         return s;
65 String
66 runtime_gostringnocopy(const byte *str)
68         String s;
69         
70         s.str = str;
71         s.len = runtime_findnull(str);
72         return s;
75 func cstringToGo(str *byte) (s String) {
76         s = runtime_gostringnocopy(str);
79 enum
81         Runeself        = 0x80,
84 func stringiter(s String, k int) (retk int) {
85         int32 l;
87         if(k >= s.len) {
88                 // retk=0 is end of iteration
89                 retk = 0;
90                 goto out;
91         }
93         l = s.str[k];
94         if(l < Runeself) {
95                 retk = k+1;
96                 goto out;
97         }
99         // multi-char rune
100         retk = k + charntorune(&l, s.str+k, s.len-k);
102 out:
105 func stringiter2(s String, k int) (retk int, retv int32) {
106         if(k >= s.len) {
107                 // retk=0 is end of iteration
108                 retk = 0;
109                 retv = 0;
110                 goto out;
111         }
113         retv = s.str[k];
114         if(retv < Runeself) {
115                 retk = k+1;
116                 goto out;
117         }
119         // multi-char rune
120         retk = k + charntorune(&retv, s.str+k, s.len-k);
122 out: