2013-02-11 Sebastian Huber <sebastian.huber@embedded-brains.de>
[official-gcc.git] / libgo / runtime / string.goc
blob04ecbe6f73ac46be1eac61d84c6961cf8841a6ca
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 static String
24 gostringsize(intgo l, byte** pmem)
26         String s;
27         byte *mem;
29         if(l == 0) {
30                 *pmem = nil;
31                 return runtime_emptystring;
32         }
33         // leave room for NUL for C runtime (e.g., callers of getenv)
34         mem = runtime_mallocgc(l+1, FlagNoPointers, 1, 0);
35         s.str = mem;
36         s.len = l;
37         mem[l] = 0;
38         *pmem = mem;
39         return s;
42 String
43 runtime_gostring(const byte *str)
45         intgo l;
46         String s;
47         byte *mem;
49         l = runtime_findnull(str);
50         s = gostringsize(l, &mem);
51         runtime_memmove(mem, str, l);
52         return s;
55 String
56 runtime_gostringnocopy(const byte *str)
58         String s;
59         
60         s.str = str;
61         s.len = runtime_findnull(str);
62         return s;
65 enum
67         Runeself        = 0x80,
70 func stringiter(s String, k int) (retk int) {
71         int32 l;
73         if(k >= s.len) {
74                 // retk=0 is end of iteration
75                 retk = 0;
76                 goto out;
77         }
79         l = s.str[k];
80         if(l < Runeself) {
81                 retk = k+1;
82                 goto out;
83         }
85         // multi-char rune
86         retk = k + charntorune(&l, s.str+k, s.len-k);
88 out:
91 func stringiter2(s String, k int) (retk int, retv int32) {
92         if(k >= s.len) {
93                 // retk=0 is end of iteration
94                 retk = 0;
95                 retv = 0;
96                 goto out;
97         }
99         retv = s.str[k];
100         if(retv < Runeself) {
101                 retk = k+1;
102                 goto out;
103         }
105         // multi-char rune
106         retk = k + charntorune(&retv, s.str+k, s.len-k);
108 out: