size_t trim_end(const char *p, size_t len) {
while (len > 0) {
if (p[len - 1] != ' ') break;
--len;
}
return len;
}
Conceptually this function accepts a slice `(p, len, cap)` and returns a slice `(p, len2, cap)` where `len2 <= len` and the capacity never changes. But the actual argument doesn't have `cap`, and the return argument doesn't have `p`. Everything is implicit and it's typical for C programmers to fully document and follow such implicits. Go's slice operator can't come out of such implicit practices in my opinion.In comparison, your claim only makes sense when the following was somehow normal:
struct slice { const char *p; size_t len, cap; };
struct slice trim_end(const struct slice *s) {
struct slice out = *s;
while (out.len > 0) {
if (out.p[out.len - 1] != ' ') break;
out = subslice(out, 0, out.len - 1);
}
return out;
}
Note that a hypothetical `subslice` function call maps perfectly to a Go code `out[0:len(out)-1]`, and my complaint will equally apply: there should be two clearly named variants of `subslice` that may or may not keep the capacity. But I hardly saw such construction in C.