// append contents to buf, it returns the length of s intwriteString(string s){ byte[] temp = toByteArray(s); append(buf, temp); return len(temp); } // append b to buf, it returns the length of b. intWriteBytes(byte[] b){ append(buf, b); return len(b); }
// copies the buffer to a new. grow(int n){ if (n > 0) { // when there is not enough space left. if (cap(buf) - len(buf) < n) { byte[] newBuf = [len(buf), 2*cap(buf)+n]{}; copy(newBuf, buf); buf = newBuf; } } }
classStrings{ // concatenates the elements of its first argument to create a single string. The separator // string sep is placed between elements in the resulting string. string join(string[] elems, string sep){ if (len(elems) == 0) { return""; } if (len(elems) == 1) { return elems[0]; } byte[] bs = toByteArray(sep); intn= len(bs) * (len(elems) -1); for (int i=0; i < len(elems); i++) { strings= elems[i]; byte[] bs = toByteArray(s); n = n + len(bs); } StringBuildersb= StringBuilder(); sb.grow(n); stringfirst= elems[0]; sb.writeString(first);