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 5package runtime 6#include "runtime.h" 7#include "arch.h" 8#include "malloc.h" 9#include "go-string.h" 10 11#define charntorune(pv, str, len) __go_get_rune(str, len, pv) 12 13const String runtime_emptystring; 14 15intgo 16runtime_findnull(const byte *s) 17{ 18 if(s == nil) 19 return 0; 20 return __builtin_strlen((const char*) s); 21} 22 23intgo 24runtime_findnullw(const uint16 *s) 25{ 26 intgo l; 27 28 if(s == nil) 29 return 0; 30 for(l=0; s[l]!=0; l++) 31 ; 32 return l; 33} 34 35static String 36gostringsize(intgo l, byte** pmem) 37{ 38 String s; 39 byte *mem; 40 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; 50} 51 52String 53runtime_gostring(const byte *str) 54{ 55 intgo l; 56 String s; 57 byte *mem; 58 59 l = runtime_findnull(str); 60 s = gostringsize(l, &mem); 61 runtime_memmove(mem, str, l); 62 return s; 63} 64 65String 66runtime_gostringnocopy(const byte *str) 67{ 68 String s; 69 70 s.str = str; 71 s.len = runtime_findnull(str); 72 return s; 73} 74 75func cstringToGo(str *byte) (s String) { 76 s = runtime_gostringnocopy(str); 77} 78 79enum 80{ 81 Runeself = 0x80, 82}; 83 84func stringiter(s String, k int) (retk int) { 85 int32 l; 86 87 if(k >= s.len) { 88 // retk=0 is end of iteration 89 retk = 0; 90 goto out; 91 } 92 93 l = s.str[k]; 94 if(l < Runeself) { 95 retk = k+1; 96 goto out; 97 } 98 99 // multi-char rune 100 retk = k + charntorune(&l, s.str+k, s.len-k); 101 102out: 103} 104 105func 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 } 112 113 retv = s.str[k]; 114 if(retv < Runeself) { 115 retk = k+1; 116 goto out; 117 } 118 119 // multi-char rune 120 retk = k + charntorune(&retv, s.str+k, s.len-k); 121 122out: 123} 124