diff options
author | Robert Griesemer <gri@golang.org> | 2008-09-18 16:58:37 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2008-09-18 16:58:37 -0700 |
commit | 9fb7da748396894cb2cb15188f82e124933b8b7a (patch) | |
tree | 3f83983bfab51a4a981b8537379eee656016617d /usr/gri/pretty/utils.go | |
parent | 2180a3dc9c5c80c097f56350539fbde490845493 (diff) | |
download | golang-9fb7da748396894cb2cb15188f82e124933b8b7a.tar.gz |
First cut at a Go pretty printer:
- code scavenged from Go-in-Go front-end (will merge back)
- using "symbol-table" free parsing to build AST
- no printing yet
R=r
OCL=15504
CL=15504
Diffstat (limited to 'usr/gri/pretty/utils.go')
-rw-r--r-- | usr/gri/pretty/utils.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/usr/gri/pretty/utils.go b/usr/gri/pretty/utils.go new file mode 100644 index 000000000..0764a2f8c --- /dev/null +++ b/usr/gri/pretty/utils.go @@ -0,0 +1,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)]; +} |