2014-07-29 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / libgo / runtime / string.goc
blobf656318d15cb6dbfa98c08e25cc121dbd9c558cc
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 intgo
25 runtime_findnullw(const uint16 *s)
27         intgo l;
29         if(s == nil)
30                 return 0;
31         for(l=0; s[l]!=0; l++)
32                 ;
33         return l;
36 static String
37 gostringsize(intgo l, byte** pmem)
39         String s;
40         byte *mem;
42         if(l == 0) {
43                 *pmem = nil;
44                 return runtime_emptystring;
45         }
46         mem = runtime_mallocgc(l, 0, FlagNoScan|FlagNoZero);
47         s.str = mem;
48         s.len = l;
49         *pmem = mem;
50         return s;
53 String
54 runtime_gostring(const byte *str)
56         intgo l;
57         String s;
58         byte *mem;
60         l = runtime_findnull(str);
61         s = gostringsize(l, &mem);
62         runtime_memmove(mem, str, l);
63         return s;
66 String
67 runtime_gostringnocopy(const byte *str)
69         String s;
70         
71         s.str = str;
72         s.len = runtime_findnull(str);
73         return s;
76 func cstringToGo(str *byte) (s String) {
77         s = runtime_gostringnocopy(str);
80 enum
82         Runeself        = 0x80,
85 func stringiter(s String, k int) (retk int) {
86         int32 l;
88         if(k >= s.len) {
89                 // retk=0 is end of iteration
90                 retk = 0;
91                 goto out;
92         }
94         l = s.str[k];
95         if(l < Runeself) {
96                 retk = k+1;
97                 goto out;
98         }
100         // multi-char rune
101         retk = k + charntorune(&l, s.str+k, s.len-k);
103 out:
106 func stringiter2(s String, k int) (retk int, retv int32) {
107         if(k >= s.len) {
108                 // retk=0 is end of iteration
109                 retk = 0;
110                 retv = 0;
111                 goto out;
112         }
114         retv = s.str[k];
115         if(retv < Runeself) {
116                 retk = k+1;
117                 goto out;
118         }
120         // multi-char rune
121         retk = k + charntorune(&retv, s.str+k, s.len-k);
123 out: