111
|
1 /* go-strslice.c -- the go string slice function.
|
|
2
|
|
3 Copyright 2009 The Go Authors. All rights reserved.
|
|
4 Use of this source code is governed by a BSD-style
|
|
5 license that can be found in the LICENSE file. */
|
|
6
|
|
7 #include "runtime.h"
|
|
8
|
|
9 String
|
|
10 __go_string_slice (String s, intgo start, intgo end)
|
|
11 {
|
|
12 intgo len;
|
|
13 String ret;
|
|
14
|
|
15 len = s.len;
|
|
16 if (end == -1)
|
|
17 end = len;
|
|
18 if (start > len || end < start || end > len)
|
|
19 runtime_panicstring ("string index out of bounds");
|
|
20 ret.len = end - start;
|
|
21 // If the length of the new string is zero, the str field doesn't
|
|
22 // matter, so just set it to nil. This avoids the problem of
|
|
23 // s.str + start pointing just past the end of the string,
|
|
24 // which may keep the next memory block alive unnecessarily.
|
|
25 if (ret.len == 0)
|
|
26 ret.str = nil;
|
|
27 else
|
|
28 ret.str = s.str + start;
|
|
29 return ret;
|
|
30 }
|