summaryrefslogtreecommitdiff
path: root/usr/gri/pretty/utils.go
blob: 0764a2f8ca256e1f8321a401476bc2ada30ad75c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 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.

package Utils


export func BaseName(s string) string {
	// TODO this is not correct for non-ASCII strings!
	i := len(s) - 1;
	for i >= 0 && s[i] != '/' {
		if s[i] > 128 {
			panic("non-ASCII string");
		}
		i--;
	}
	return s[i + 1 : len(s)];
}


export func Contains(s, sub string, pos int) bool {
	end := pos + len(sub);
	return pos >= 0 && end <= len(s) && s[pos : end] == sub;
}


export func TrimExt(s, ext string) string {
	i := len(s) - len(ext);
	if i >= 0 && s[i : len(s)] == ext {
		s = s[0 : i];
	}
	return s;
}


export func IntToString(x, base int) string {
	x0 := x;
	if x < 0 {
		x = -x;
		if x < 0 {
			panic("smallest int not handled");
		}
	} else if x == 0 {
		return "0";
	}

	// x > 0
	hex := "0123456789ABCDEF";
	var buf [32] byte;
	i := len(buf);
	for x > 0 {
		i--;
		buf[i] = hex[x % base];
		x /= base;
	}
	
	if x0 < 0 {
		i--;
		buf[i] = '-';
	}
	
	return string(buf)[i : len(buf)];
}