// 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 ()type file struct { file *os.File data []byte atEOF bool}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 open( string) (*file, error) { , := os.Open()if != nil {returnnil, }return &file{, make([]byte, 0, 64*1024), false}, nil}func stat( 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 countAnyByte( string, string) int { := 0for := 0; < len(); ++ {ifbytealg.IndexByteString(, []) >= 0 { ++ } }return}// Split s at any bytes in t.func splitAtBytes( string, string) []string { := make([]string, 1+countAnyByte(, )) := 0 := 0for := 0; < len(); ++ {ifbytealg.IndexByteString(, []) >= 0 {if < { [] = [:] ++ } = + 1 } }if < len() { [] = [:] ++ }return [0:]}func getFields( string) []string { returnsplitAtBytes(, " \r\t\n") }// Bigger than we need, not too big to worry about overflowconst big = 0xFFFFFF// Decimal to integer.// Returns number, characters consumed, success.func dtoi( 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 xtoi( 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 xtoi2( string, byte) (byte, bool) {iflen() > 2 && [2] != {return0, false } , , := xtoi([:2])returnbyte(), && == 2}// hasUpperCase tells whether the given string contains at least one upper-case.func hasUpperCase( string) bool {for := range {if'A' <= [] && [] <= 'Z' {returntrue } }returnfalse}// lowerASCIIBytes makes x ASCII lowercase in-place.func lowerASCIIBytes( []byte) {for , := range {if'A' <= && <= 'Z' { [] += 'a' - 'A' } }}// lowerASCII returns the ASCII lowercase version of b.func lowerASCII( byte) byte {if'A' <= && <= 'Z' {return + ('a' - 'A') }return}// trimSpace returns x without any leading or trailing ASCII whitespace.func trimSpace( 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 isSpace( byte) bool {return == ' ' || == '\t' || == '\n' || == '\r'}// removeComment returns line, removing any '#' byte and any following// bytes.func removeComment( 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 foreachField( string, func( string) error) error { = trimSpace()forlen() > 0 { := bytealg.IndexByteString(, ' ')if == -1 {return () }if := trimSpace([:]); len() > 0 {if := (); != nil {return } } = trimSpace([+1:]) }returnnil}// stringsHasSuffixFold reports whether s ends in suffix,// ASCII-case-insensitively.func stringsHasSuffixFold(, string) bool {returnlen() >= len() && stringsEqualFold([len()-len():], )}// stringsEqualFold is strings.EqualFold, ASCII only. It reports whether s and t// are equal, ASCII-case-insensitively.func stringsEqualFold(, string) bool {iflen() != len() {returnfalse }for := 0; < len(); ++ {iflowerASCII([]) != lowerASCII([]) {returnfalse } }returntrue}
The pages are generated with Goldsv0.7.0-preview. (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 @zigo_101 (reachable from the left QR code) to get the latest news of Golds.