| 
									
										
										
										
											2008-07-29 19:02:49 -07:00
										 |  |  | // 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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-04 15:37:47 -07:00
										 |  |  | export func BaseName(s string) string { | 
					
						
							| 
									
										
										
										
											2008-07-29 19:02:49 -07:00
										 |  |  | 	// TODO this is not correct for non-ASCII strings! | 
					
						
							|  |  |  | 	i := len(s) - 1; | 
					
						
							|  |  |  | 	for i >= 0 && s[i] != '/' { | 
					
						
							|  |  |  | 		if s[i] > 128 { | 
					
						
							| 
									
										
										
										
											2008-08-11 21:20:42 -07:00
										 |  |  | 			panic("non-ASCII string"); | 
					
						
							| 
									
										
										
										
											2008-07-29 19:02:49 -07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		i--; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return s[i + 1 : len(s)]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-11 09:45:40 -07:00
										 |  |  | export func Contains(s, sub string, pos int) bool { | 
					
						
							|  |  |  | 	end := pos + len(sub); | 
					
						
							|  |  |  | 	return pos >= 0 && end <= len(s) && s[pos : end] == sub; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-07 19:32:22 -07:00
										 |  |  | export func TrimExt(s, ext string) string { | 
					
						
							|  |  |  | 	i := len(s) - len(ext); | 
					
						
							|  |  |  | 	if i >= 0 && s[i : len(s)] == ext { | 
					
						
							| 
									
										
										
										
											2008-07-29 19:02:49 -07:00
										 |  |  | 		s = s[0 : i]; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-08-07 19:32:22 -07:00
										 |  |  | 	return s; | 
					
						
							| 
									
										
										
										
											2008-07-30 17:36:03 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-08-11 09:45:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export func IntToString(x, base int) string { | 
					
						
							|  |  |  | 	x0 := x; | 
					
						
							|  |  |  | 	if x < 0 { | 
					
						
							|  |  |  | 		x = -x; | 
					
						
							|  |  |  | 		if x < 0 { | 
					
						
							| 
									
										
										
										
											2008-08-11 21:20:42 -07:00
										 |  |  | 			panic("smallest int not handled"); | 
					
						
							| 
									
										
										
										
											2008-08-11 09:45:40 -07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} 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)]; | 
					
						
							|  |  |  | } |