2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009, 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
#include "runtime.h"
|
2012-01-13 06:11:45 +01:00
|
|
|
#include "arch.h"
|
|
|
|
#include "malloc.h"
|
2012-11-01 04:02:13 +01:00
|
|
|
#include "go-string.h"
|
2012-01-13 06:11:45 +01:00
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
#define charntorune(pv, str, len) __go_get_rune(str, len, pv)
|
|
|
|
|
2013-01-30 23:24:40 +01:00
|
|
|
const String runtime_emptystring;
|
|
|
|
|
2012-11-01 04:02:13 +01:00
|
|
|
intgo
|
2011-11-14 23:26:45 +01:00
|
|
|
runtime_findnull(const byte *s)
|
|
|
|
{
|
|
|
|
if(s == nil)
|
|
|
|
return 0;
|
|
|
|
return __builtin_strlen((const char*) s);
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
intgo
|
|
|
|
runtime_findnullw(const uint16 *s)
|
|
|
|
{
|
|
|
|
intgo l;
|
|
|
|
|
|
|
|
if(s == nil)
|
|
|
|
return 0;
|
|
|
|
for(l=0; s[l]!=0; l++)
|
|
|
|
;
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2013-01-30 23:24:40 +01:00
|
|
|
static String
|
|
|
|
gostringsize(intgo l, byte** pmem)
|
|
|
|
{
|
|
|
|
String s;
|
|
|
|
byte *mem;
|
|
|
|
|
|
|
|
if(l == 0) {
|
|
|
|
*pmem = nil;
|
|
|
|
return runtime_emptystring;
|
|
|
|
}
|
2014-06-07 00:37:27 +02:00
|
|
|
mem = runtime_mallocgc(l, 0, FlagNoScan|FlagNoZero);
|
2013-01-30 23:24:40 +01:00
|
|
|
s.str = mem;
|
|
|
|
s.len = l;
|
|
|
|
*pmem = mem;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
String
|
|
|
|
runtime_gostring(const byte *str)
|
|
|
|
{
|
|
|
|
intgo l;
|
|
|
|
String s;
|
|
|
|
byte *mem;
|
|
|
|
|
|
|
|
l = runtime_findnull(str);
|
|
|
|
s = gostringsize(l, &mem);
|
|
|
|
runtime_memmove(mem, str, l);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-11-14 23:26:45 +01:00
|
|
|
String
|
2011-11-30 01:21:52 +01:00
|
|
|
runtime_gostringnocopy(const byte *str)
|
2011-11-14 23:26:45 +01:00
|
|
|
{
|
|
|
|
String s;
|
|
|
|
|
2012-11-01 04:02:13 +01:00
|
|
|
s.str = str;
|
|
|
|
s.len = runtime_findnull(str);
|
2011-11-14 23:26:45 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func cstringToGo(str *byte) (s String) {
|
|
|
|
s = runtime_gostringnocopy(str);
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
Runeself = 0x80,
|
|
|
|
};
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
func stringiter(s String, k int) (retk int) {
|
|
|
|
int32 l;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-11-01 04:02:13 +01:00
|
|
|
if(k >= s.len) {
|
2010-12-03 05:34:57 +01:00
|
|
|
// retk=0 is end of iteration
|
|
|
|
retk = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-11-01 04:02:13 +01:00
|
|
|
l = s.str[k];
|
2010-12-03 05:34:57 +01:00
|
|
|
if(l < Runeself) {
|
|
|
|
retk = k+1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// multi-char rune
|
2012-11-01 04:02:13 +01:00
|
|
|
retk = k + charntorune(&l, s.str+k, s.len-k);
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
out:
|
|
|
|
}
|
|
|
|
|
2012-11-06 19:28:21 +01:00
|
|
|
func stringiter2(s String, k int) (retk int, retv int32) {
|
2012-11-01 04:02:13 +01:00
|
|
|
if(k >= s.len) {
|
2010-12-03 05:34:57 +01:00
|
|
|
// retk=0 is end of iteration
|
|
|
|
retk = 0;
|
|
|
|
retv = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-11-01 04:02:13 +01:00
|
|
|
retv = s.str[k];
|
2010-12-03 05:34:57 +01:00
|
|
|
if(retv < Runeself) {
|
|
|
|
retk = k+1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// multi-char rune
|
2012-11-01 04:02:13 +01:00
|
|
|
retk = k + charntorune(&retv, s.str+k, s.len-k);
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
out:
|
|
|
|
}
|