2008-06-30 12:50:36 -06:00
|
|
|
// Copyright 2009 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.
|
|
|
|
|
2009-06-30 21:01:50 -06:00
|
|
|
package sys
|
2008-06-30 12:50:36 -06:00
|
|
|
#include "runtime.h"
|
|
|
|
|
2009-04-09 19:16:21 -06:00
|
|
|
String emptystring;
|
2008-06-30 12:50:36 -06:00
|
|
|
|
|
|
|
int32
|
2008-11-23 18:08:55 -07:00
|
|
|
findnull(byte *s)
|
2008-06-30 12:50:36 -06:00
|
|
|
{
|
|
|
|
int32 l;
|
|
|
|
|
2009-02-02 19:59:20 -07:00
|
|
|
if(s == nil)
|
|
|
|
return 0;
|
2008-06-30 12:50:36 -06:00
|
|
|
for(l=0; s[l]!=0; l++)
|
|
|
|
;
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2009-01-29 18:38:58 -07:00
|
|
|
int32 maxstring;
|
|
|
|
|
2009-04-09 19:16:21 -06:00
|
|
|
String
|
2009-01-29 18:38:58 -07:00
|
|
|
gostringsize(int32 l)
|
|
|
|
{
|
2009-04-09 19:16:21 -06:00
|
|
|
String s;
|
2009-01-29 18:38:58 -07:00
|
|
|
|
2009-04-09 19:16:21 -06:00
|
|
|
if(l == 0)
|
|
|
|
return emptystring;
|
2009-05-26 18:39:25 -06:00
|
|
|
s.str = mal(l+1); // leave room for NUL for C runtime (e.g., callers of getenv)
|
2009-04-09 19:16:21 -06:00
|
|
|
s.len = l;
|
2009-01-29 18:38:58 -07:00
|
|
|
if(l > maxstring)
|
|
|
|
maxstring = l;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-04-09 19:16:21 -06:00
|
|
|
String
|
2008-11-23 18:08:55 -07:00
|
|
|
gostring(byte *str)
|
|
|
|
{
|
|
|
|
int32 l;
|
2009-04-09 19:16:21 -06:00
|
|
|
String s;
|
2008-11-23 18:08:55 -07:00
|
|
|
|
|
|
|
l = findnull(str);
|
2009-01-29 18:38:58 -07:00
|
|
|
s = gostringsize(l);
|
2009-04-09 19:16:21 -06:00
|
|
|
mcpy(s.str, str, l);
|
2008-11-23 18:08:55 -07:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-06-30 21:01:50 -06:00
|
|
|
func catstring(s1 String, s2 String) (s3 String) {
|
2009-04-09 19:16:21 -06:00
|
|
|
if(s1.len == 0) {
|
2008-06-30 12:50:36 -06:00
|
|
|
s3 = s2;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-04-09 19:16:21 -06:00
|
|
|
if(s2.len == 0) {
|
2008-06-30 12:50:36 -06:00
|
|
|
s3 = s1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-04-09 19:16:21 -06:00
|
|
|
s3 = gostringsize(s1.len + s2.len);
|
|
|
|
mcpy(s3.str, s1.str, s1.len);
|
|
|
|
mcpy(s3.str+s1.len, s2.str, s2.len);
|
2008-06-30 12:50:36 -06:00
|
|
|
out:
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
prbounds(int8* s, int32 a, int32 b, int32 c)
|
|
|
|
{
|
|
|
|
prints(s);
|
|
|
|
prints(" ");
|
|
|
|
sys·printint(a);
|
|
|
|
prints("<");
|
|
|
|
sys·printint(b);
|
|
|
|
prints(">");
|
|
|
|
sys·printint(c);
|
|
|
|
prints("\n");
|
2008-09-18 16:56:46 -06:00
|
|
|
throw("string bounds");
|
2008-06-30 12:50:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32
|
2009-04-09 19:16:21 -06:00
|
|
|
cmpstring(String s1, String s2)
|
2008-06-30 12:50:36 -06:00
|
|
|
{
|
|
|
|
uint32 i, l;
|
|
|
|
byte c1, c2;
|
|
|
|
|
2009-04-09 19:16:21 -06:00
|
|
|
l = s1.len;
|
|
|
|
if(s2.len < l)
|
|
|
|
l = s2.len;
|
2008-06-30 12:50:36 -06:00
|
|
|
for(i=0; i<l; i++) {
|
2009-04-09 19:16:21 -06:00
|
|
|
c1 = s1.str[i];
|
|
|
|
c2 = s2.str[i];
|
2008-06-30 12:50:36 -06:00
|
|
|
if(c1 < c2)
|
|
|
|
return -1;
|
|
|
|
if(c1 > c2)
|
|
|
|
return +1;
|
|
|
|
}
|
2009-04-09 19:16:21 -06:00
|
|
|
if(s1.len < s2.len)
|
2008-06-30 12:50:36 -06:00
|
|
|
return -1;
|
2009-04-09 19:16:21 -06:00
|
|
|
if(s1.len > s2.len)
|
2008-06-30 12:50:36 -06:00
|
|
|
return +1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-30 21:01:50 -06:00
|
|
|
func cmpstring(s1 String, s2 String) (v int32) {
|
2008-06-30 12:50:36 -06:00
|
|
|
v = cmpstring(s1, s2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32
|
|
|
|
strcmp(byte *s1, byte *s2)
|
|
|
|
{
|
|
|
|
uint32 i;
|
|
|
|
byte c1, c2;
|
|
|
|
|
|
|
|
for(i=0;; i++) {
|
|
|
|
c1 = s1[i];
|
|
|
|
c2 = s2[i];
|
|
|
|
if(c1 < c2)
|
|
|
|
return -1;
|
|
|
|
if(c1 > c2)
|
|
|
|
return +1;
|
|
|
|
if(c1 == 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-30 21:01:50 -06:00
|
|
|
func slicestring(si String, lindex int32, hindex int32) (so String) {
|
2008-06-30 12:50:36 -06:00
|
|
|
int32 l;
|
|
|
|
|
2009-04-09 19:16:21 -06:00
|
|
|
if(lindex < 0 || lindex > si.len ||
|
|
|
|
hindex < lindex || hindex > si.len) {
|
2008-06-30 12:50:36 -06:00
|
|
|
sys·printpc(&si);
|
|
|
|
prints(" ");
|
2009-04-09 19:16:21 -06:00
|
|
|
prbounds("slice", lindex, si.len, hindex);
|
2008-06-30 12:50:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
l = hindex-lindex;
|
2009-04-09 19:16:21 -06:00
|
|
|
so.str = si.str + lindex;
|
|
|
|
so.len = l;
|
|
|
|
|
|
|
|
// alternate to create a new string
|
|
|
|
// so = gostringsize(l);
|
|
|
|
// mcpy(so.str, si.str+lindex, l);
|
2008-06-30 12:50:36 -06:00
|
|
|
}
|
|
|
|
|
2009-06-30 21:01:50 -06:00
|
|
|
func indexstring(s String, i int32) (b byte) {
|
2009-04-09 19:16:21 -06:00
|
|
|
if(i < 0 || i >= s.len) {
|
2008-06-30 12:50:36 -06:00
|
|
|
sys·printpc(&s);
|
|
|
|
prints(" ");
|
2009-04-09 19:16:21 -06:00
|
|
|
prbounds("index", 0, i, s.len);
|
2008-06-30 12:50:36 -06:00
|
|
|
}
|
|
|
|
|
2009-04-09 19:16:21 -06:00
|
|
|
b = s.str[i];
|
2008-06-30 12:50:36 -06:00
|
|
|
}
|
|
|
|
|
2009-06-30 21:01:50 -06:00
|
|
|
func intstring(v int64) (s String) {
|
2009-01-29 18:38:58 -07:00
|
|
|
s = gostringsize(8);
|
2009-04-09 19:16:21 -06:00
|
|
|
s.len = runetochar(s.str, v);
|
2008-06-30 12:50:36 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 16:54:25 -06:00
|
|
|
func slicebytetostring(b Slice) (s String) {
|
|
|
|
s = gostringsize(b.len);
|
2009-04-09 19:16:21 -06:00
|
|
|
mcpy(s.str, b.array, s.len);
|
2008-09-22 21:12:15 -06:00
|
|
|
}
|
2009-04-10 20:49:31 -06:00
|
|
|
|
2009-06-30 21:01:50 -06:00
|
|
|
|
2009-08-25 16:54:25 -06:00
|
|
|
func sliceinttostring(b Slice) (s String) {
|
2009-05-27 16:56:44 -06:00
|
|
|
int32 siz1, siz2, i;
|
2009-05-27 16:38:02 -06:00
|
|
|
int32 *a;
|
|
|
|
byte dum[8];
|
|
|
|
|
|
|
|
a = (int32*)b.array;
|
2009-05-27 16:56:44 -06:00
|
|
|
siz1 = 0;
|
2009-08-25 16:54:25 -06:00
|
|
|
for(i=0; i<b.len; i++) {
|
2009-05-27 16:56:44 -06:00
|
|
|
siz1 += runetochar(dum, a[i]);
|
2009-05-27 16:38:02 -06:00
|
|
|
}
|
|
|
|
|
2009-05-27 16:56:44 -06:00
|
|
|
s = gostringsize(siz1+4);
|
|
|
|
siz2 = 0;
|
2009-08-25 16:54:25 -06:00
|
|
|
for(i=0; i<b.len; i++) {
|
2009-05-27 16:56:44 -06:00
|
|
|
// check for race
|
|
|
|
if(siz2 >= siz1)
|
|
|
|
break;
|
|
|
|
siz2 += runetochar(s.str+siz2, a[i]);
|
2009-05-27 16:38:02 -06:00
|
|
|
}
|
2009-05-27 16:56:44 -06:00
|
|
|
s.len = siz2;
|
2009-05-27 16:38:02 -06:00
|
|
|
}
|
|
|
|
|
2009-04-10 20:49:31 -06:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
Runeself = 0x80,
|
|
|
|
};
|
|
|
|
|
2009-06-30 21:01:50 -06:00
|
|
|
func stringiter(s String, k int32) (retk int32) {
|
2009-04-12 23:34:36 -06:00
|
|
|
int32 l;
|
2009-04-10 20:49:31 -06:00
|
|
|
|
|
|
|
if(k >= s.len) {
|
|
|
|
// retk=0 is end of iteration
|
|
|
|
retk = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
l = s.str[k];
|
2009-04-12 23:34:36 -06:00
|
|
|
if(l < Runeself) {
|
|
|
|
retk = k+1;
|
|
|
|
goto out;
|
2009-04-10 20:49:31 -06:00
|
|
|
}
|
|
|
|
|
2009-04-12 23:34:36 -06:00
|
|
|
// multi-char rune
|
|
|
|
retk = k + charntorune(&l, s.str+k, s.len-k);
|
2009-04-10 20:49:31 -06:00
|
|
|
|
|
|
|
out:
|
|
|
|
}
|
|
|
|
|
2009-06-30 21:01:50 -06:00
|
|
|
func stringiter2(s String, k int32) (retk int32, retv int32) {
|
2009-04-10 20:49:31 -06:00
|
|
|
if(k >= s.len) {
|
|
|
|
// retk=0 is end of iteration
|
|
|
|
retk = 0;
|
|
|
|
retv = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-04-12 23:34:36 -06:00
|
|
|
retv = s.str[k];
|
|
|
|
if(retv < Runeself) {
|
|
|
|
retk = k+1;
|
|
|
|
goto out;
|
2009-04-10 20:49:31 -06:00
|
|
|
}
|
|
|
|
|
2009-04-12 23:34:36 -06:00
|
|
|
// multi-char rune
|
|
|
|
retk = k + charntorune(&retv, s.str+k, s.len-k);
|
2009-04-10 20:49:31 -06:00
|
|
|
|
|
|
|
out:
|
|
|
|
}
|