2013-09-12 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgo / runtime / string.goc
blob64ed4f6ebaa34f9bec3c99ecc9e76e123c83dabd
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"
10 #include "race.h"
12 #define charntorune(pv, str, len) __go_get_rune(str, len, pv)
14 const String    runtime_emptystring;
16 intgo
17 runtime_findnull(const byte *s)
19         if(s == nil)
20                 return 0;
21         return __builtin_strlen((const char*) s);
24 static String
25 gostringsize(intgo l, byte** pmem)
27         String s;
28         byte *mem;
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;
43 String
44 runtime_gostring(const byte *str)
46         intgo l;
47         String s;
48         byte *mem;
50         l = runtime_findnull(str);
51         s = gostringsize(l, &mem);
52         runtime_memmove(mem, str, l);
53         return s;
56 String
57 runtime_gostringnocopy(const byte *str)
59         String s;
60         
61         s.str = str;
62         s.len = runtime_findnull(str);
63         return s;
66 enum
68         Runeself        = 0x80,
71 func stringiter(s String, k int) (retk int) {
72         int32 l;
74         if(k >= s.len) {
75                 // retk=0 is end of iteration
76                 retk = 0;
77                 goto out;
78         }
80         l = s.str[k];
81         if(l < Runeself) {
82                 retk = k+1;
83                 goto out;
84         }
86         // multi-char rune
87         retk = k + charntorune(&l, s.str+k, s.len-k);
89 out:
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         }
100         retv = s.str[k];
101         if(retv < Runeself) {
102                 retk = k+1;
103                 goto out;
104         }
106         // multi-char rune
107         retk = k + charntorune(&retv, s.str+k, s.len-k);
109 out: