// 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 net

import (
	
	
	
	
)

type file struct {
	file  *os.File
	data  []byte
	atEOF bool
}

func ( *file) () { .file.Close() }

func ( *file) () ( string,  bool) {
	 := .data
	 := 0
	for  = 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
	}
	if len(.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 {
		return time.Time{}, 0, 
	}
	return .ModTime(), .Size(), nil
}

func ( string) (*file, error) {
	,  := os.Open()
	if  != nil {
		return nil, 
	}
	return &file{, make([]byte, 0, 64*1024), false}, nil
}

func ( string) ( time.Time,  int64,  error) {
	,  := os.Stat()
	if  != nil {
		return time.Time{}, 0, 
	}
	return .ModTime(), .Size(), nil
}

// Count occurrences in s of any bytes in t.
func ( string,  string) int {
	 := 0
	for  := 0;  < len(); ++ {
		if bytealg.IndexByteString(, []) >= 0 {
			++
		}
	}
	return 
}

// Split s at any bytes in t.
func ( string,  string) []string {
	 := make([]string, 1+countAnyByte(, ))
	 := 0
	 := 0
	for  := 0;  < len(); ++ {
		if bytealg.IndexByteString(, []) >= 0 {
			if  <  {
				[] = [:]
				++
			}
			 =  + 1
		}
	}
	if  < len() {
		[] = [:]
		++
	}
	return [0:]
}

func ( string) []string { return splitAtBytes(, " \r\t\n") }

// Bigger than we need, not too big to worry about overflow
const big = 0xFFFFFF

// Decimal to integer.
// Returns number, characters consumed, success.
func ( string) ( int,  int,  bool) {
	 = 0
	for  = 0;  < len() && '0' <= [] && [] <= '9'; ++ {
		 = *10 + int([]-'0')
		if  >= big {
			return big, , false
		}
	}
	if  == 0 {
		return 0, 0, false
	}
	return , , true
}

// Hexadecimal to integer.
// Returns number, characters consumed, success.
func ( string) ( int,  int,  bool) {
	 = 0
	for  = 0;  < len(); ++ {
		if '0' <= [] && [] <= '9' {
			 *= 16
			 += int([] - '0')
		} else if 'a' <= [] && [] <= 'f' {
			 *= 16
			 += int([]-'a') + 10
		} else if 'A' <= [] && [] <= 'F' {
			 *= 16
			 += int([]-'A') + 10
		} else {
			break
		}
		if  >= big {
			return 0, , false
		}
	}
	if  == 0 {
		return 0, , 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) {
	if len() > 2 && [2] !=  {
		return 0, false
	}
	, ,  := xtoi([:2])
	return byte(),  &&  == 2
}

// Convert i to a hexadecimal string. Leading zeros are not printed.
func ( []byte,  uint32) []byte {
	if  == 0 {
		return append(, '0')
	}
	for  := 7;  >= 0; -- {
		 :=  >> uint(*4)
		if  > 0 {
			 = append(, hexDigit[&0xf])
		}
	}
	return 
}

// Number of occurrences of b in s.
func ( string,  byte) int {
	 := 0
	for  := 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' {
			return true
		}
	}
	return false
}

// 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 {
	for len() > 0 && isSpace([0]) {
		 = [1:]
	}
	for len() > 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()
	for len() > 0 {
		 := bytealg.IndexByteString(, ' ')
		if  == -1 {
			return ()
		}
		if  := trimSpace([:]); len() > 0 {
			if  := ();  != nil {
				return 
			}
		}
		 = trimSpace([+1:])
	}
	return nil
}

// stringsHasSuffix is strings.HasSuffix. It reports whether s ends in
// suffix.
func (,  string) bool {
	return len() >= len() && [len()-len():] == 
}

// stringsHasSuffixFold reports whether s ends in suffix,
// ASCII-case-insensitively.
func (,  string) bool {
	return len() >= len() && stringsEqualFold([len()-len():], )
}

// stringsHasPrefix is strings.HasPrefix. It reports whether s begins with prefix.
func (,  string) bool {
	return len() >= len() && [:len()] == 
}

// stringsEqualFold is strings.EqualFold, ASCII only. It reports whether s and t
// are equal, ASCII-case-insensitively.
func (,  string) bool {
	if len() != len() {
		return false
	}
	for  := 0;  < len(); ++ {
		if lowerASCII([]) != lowerASCII([]) {
			return false
		}
	}
	return true
}