]> Cypherpunks repositories - gostls13.git/commitdiff
x[y:] for strings
authorRuss Cox <rsc@golang.org>
Fri, 20 Nov 2009 19:42:28 +0000 (11:42 -0800)
committerRuss Cox <rsc@golang.org>
Fri, 20 Nov 2009 19:42:28 +0000 (11:42 -0800)
R=ken2
https://golang.org/cl/157114

src/cmd/gc/builtin.c.boot
src/cmd/gc/const.c
src/cmd/gc/runtime.go
src/cmd/gc/walk.c
src/pkg/runtime/string.cgo
test/ken/string.go

index 58d6f9e82858450375848e85ac2762f43d9d2ab7..d2ff0ff90e41c66e4c72e0818dd742ad1b96215c 100644 (file)
@@ -19,6 +19,7 @@ char *runtimeimport =
        "func runtime.catstring (? string, ? string) (? string)\n"
        "func runtime.cmpstring (? string, ? string) (? int)\n"
        "func runtime.slicestring (? string, ? int, ? int) (? string)\n"
+       "func runtime.slicestring1 (? string, ? int) (? string)\n"
        "func runtime.indexstring (? string, ? int) (? uint8)\n"
        "func runtime.intstring (? int64) (? string)\n"
        "func runtime.slicebytetostring (? []uint8) (? string)\n"
index cca13b9528988dd52ea16c03c6054420a84f0803..4575ff6da45c85c840e7193c0b72a299b1b1a8bc 100644 (file)
@@ -55,7 +55,7 @@ truncfltlit(Mpflt *oldv, Type *t)
 void
 convlit(Node **np, Type *t)
 {
-       return convlit1(np, t, 0);
+       convlit1(np, t, 0);
 }
 
 /*
index ea4084012c75688bf1655060b5843c4337936327..6413db5e2177c508fcf93df0e319c203b90cd575 100644 (file)
@@ -27,6 +27,7 @@ func printsp()
 func catstring(string, string) string
 func cmpstring(string, string) int
 func slicestring(string, int, int) string
+func slicestring1(string, int) string
 func indexstring(string, int) byte
 func intstring(int64) string
 func slicebytetostring([]byte) string
index bf35b38917fa5c608d7cb96d5dcf46e889f0042b..3c3a00cfd6df961f7ee4387317b9621d66d2d56d 100644 (file)
@@ -918,10 +918,16 @@ walkexpr(Node **np, NodeList **init)
 
        case OSLICESTR:
                // sys_slicestring(s, lb, hb)
-               n = mkcall("slicestring", n->type, init,
-                       conv(n->left, types[TSTRING]),
-                       conv(n->right->left, types[TINT]),
-                       conv(n->right->right, types[TINT]));
+               if(n->right->right) {
+                       n = mkcall("slicestring", n->type, init,
+                               conv(n->left, types[TSTRING]),
+                               conv(n->right->left, types[TINT]),
+                               conv(n->right->right, types[TINT]));
+               } else {
+                       n = mkcall("slicestring1", n->type, init,
+                               conv(n->left, types[TSTRING]),
+                               conv(n->right->left, types[TINT]));
+               }
                goto ret;
 
        case OINDEXSTR:
index bafa6791b00cdc893c1a2dae3b6e3e3e6da14efe..6e380a1075a2c5b874b8cc57a6d314535fb6c0d5 100644 (file)
@@ -142,6 +142,24 @@ func slicestring(si String, lindex int32, hindex int32) (so String) {
 //     mcpy(so.str, si.str+lindex, l);
 }
 
+func slicestring1(si String, lindex int32) (so String) {
+       int32 l;
+
+       if(lindex < 0 || lindex > si.len) {
+               runtime·printpc(&si);
+               prints(" ");
+               prbounds("slice", lindex, si.len, si.len);
+       }
+
+       l = si.len-lindex;
+       so.str = si.str + lindex;
+       so.len = l;
+
+//     alternate to create a new string
+//     so = gostringsize(l);
+//     mcpy(so.str, si.str+lindex, l);
+}
+
 func indexstring(s String, i int32) (b byte) {
        if(i < 0 || i >= s.len) {
                runtime·printpc(&s);
index f7c02822f1d751aed1f6bf7926453d8a25076a9c..03e81a05d57422da0d6bdc6b02ab9e61be63ef64 100644 (file)
@@ -64,7 +64,7 @@ main()
        }
 
        /* slice strings */
-       print(c[0:3], c[3:6]);
+       print(c[0:3], c[3:]);
 
        print("\n");