// 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.// Simple file i/o and string manipulation, to avoid// depending on strconv and bufio and strings.package netimport ()typefilestruct {file *os.Filedata []byteatEOFbool}func ( *file) () { .file.Close() }func ( *file) () ( string, bool) { := .data := 0for = 0; < len(); ++ {if [] == '\n' { = string([0:]) = true// move data ++ := len() - copy([0:], [:]) .data = [0:]return } }if .atEOF && len(.data) > 0 {// EOF, return all we have = string() .data = .data[0:0] = true }return}func ( *file) () ( string, bool) {if , = .getLineFromData(); {return }iflen(.data) < cap(.data) { := len(.data) , := io.ReadFull(.file, .data[:cap(.data)])if >= 0 { .data = .data[0 : +] }if == io.EOF || == io.ErrUnexpectedEOF { .atEOF = true } } , = .getLineFromData()return}func ( *file) () ( time.Time, int64, error) { , := .file.Stat()if != nil {returntime.Time{}, 0, }return .ModTime(), .Size(), nil}func ( string) (*file, error) { , := os.Open()if != nil {returnnil, }return &file{, make([]byte, 0, 64*1024), false}, nil}func ( string) ( time.Time, int64, error) { , := os.Stat()if != nil {returntime.Time{}, 0, }return .ModTime(), .Size(), nil}// Count occurrences in s of any bytes in t.func ( string, string) int { := 0for := 0; < len(); ++ {ifbytealg.IndexByteString(, []) >= 0 { ++ } }return}// Split s at any bytes in t.func ( string, string) []string { := make([]string, 1+countAnyByte(, )) := 0 := 0for := 0; < len(); ++ {ifbytealg.IndexByteString(, []) >= 0 {if < { [] = [:] ++ } = + 1 } }if < len() { [] = [:] ++ }return [0:]}func ( string) []string { returnsplitAtBytes(, " \r\t\n") }// Bigger than we need, not too big to worry about overflowconstbig = 0xFFFFFF// Decimal to integer.// Returns number, characters consumed, success.func ( string) ( int, int, bool) { = 0for = 0; < len() && '0' <= [] && [] <= '9'; ++ { = *10 + int([]-'0')if >= big {returnbig, , false } }if == 0 {return0, 0, false }return , , true}// Hexadecimal to integer.// Returns number, characters consumed, success.func ( string) ( int, int, bool) { = 0for = 0; < len(); ++ {if'0' <= [] && [] <= '9' { *= 16 += int([] - '0') } elseif'a' <= [] && [] <= 'f' { *= 16 += int([]-'a') + 10 } elseif'A' <= [] && [] <= 'F' { *= 16 += int([]-'A') + 10 } else {break }if >= big {return0, , false } }if == 0 {return0, , false }return , , true}// xtoi2 converts the next two hex digits of s into a byte.// If s is longer than 2 bytes then the third byte must be e.// If the first two bytes of s are not hex digits or the third byte// does not match e, false is returned.func ( string, byte) (byte, bool) {iflen() > 2 && [2] != {return0, false } , , := xtoi([:2])returnbyte(), && == 2}// Convert i to a hexadecimal string. Leading zeros are not printed.func ( []byte, uint32) []byte {if == 0 {returnappend(, '0') }for := 7; >= 0; -- { := >> uint(*4)if > 0 { = append(, hexDigit[&0xf]) } }return}// Number of occurrences of b in s.func ( string, byte) int { := 0for := 0; < len(); ++ {if [] == { ++ } }return}// Index of rightmost occurrence of b in s.func ( string, byte) int { := len()for --; >= 0; -- {if [] == {break } }return}// hasUpperCase tells whether the given string contains at least one upper-case.func ( string) bool {for := range {if'A' <= [] && [] <= 'Z' {returntrue } }returnfalse}// lowerASCIIBytes makes x ASCII lowercase in-place.func ( []byte) {for , := range {if'A' <= && <= 'Z' { [] += 'a' - 'A' } }}// lowerASCII returns the ASCII lowercase version of b.func ( byte) byte {if'A' <= && <= 'Z' {return + ('a' - 'A') }return}// trimSpace returns x without any leading or trailing ASCII whitespace.func ( string) string {forlen() > 0 && isSpace([0]) { = [1:] }forlen() > 0 && isSpace([len()-1]) { = [:len()-1] }return}// isSpace reports whether b is an ASCII space character.func ( byte) bool {return == ' ' || == '\t' || == '\n' || == '\r'}// removeComment returns line, removing any '#' byte and any following// bytes.func ( string) string {if := bytealg.IndexByteString(, '#'); != -1 {return [:] }return}// foreachField runs fn on each non-empty run of non-space bytes in x.// It returns the first non-nil error returned by fn.func ( string, func( string) error) error { = trimSpace()forlen() > 0 { := bytealg.IndexByteString(, ' ')if == -1 {return () }if := trimSpace([:]); len() > 0 {if := (); != nil {return } } = trimSpace([+1:]) }returnnil}// stringsHasSuffix is strings.HasSuffix. It reports whether s ends in// suffix.func (, string) bool {returnlen() >= len() && [len()-len():] == }// stringsHasSuffixFold reports whether s ends in suffix,// ASCII-case-insensitively.func (, string) bool {returnlen() >= len() && stringsEqualFold([len()-len():], )}// stringsHasPrefix is strings.HasPrefix. It reports whether s begins with prefix.func (, string) bool {returnlen() >= len() && [:len()] == }// stringsEqualFold is strings.EqualFold, ASCII only. It reports whether s and t// are equal, ASCII-case-insensitively.func (, string) bool {iflen() != len() {returnfalse }for := 0; < len(); ++ {iflowerASCII([]) != lowerASCII([]) {returnfalse } }returntrue}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.