Skip to content

Instantly share code, notes, and snippets.

@tnoda
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tnoda/e4563f508d7c73669427 to your computer and use it in GitHub Desktop.
Save tnoda/e4563f508d7c73669427 to your computer and use it in GitHub Desktop.
cf.go
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"strconv"
)
func main() {
sc := NewScanner(os.Stdin)
//
}
////////////////////////////////////////////////////////////////
//
// gojus: retrieved from https://github.com/tnoda/gojus
//
// NewScanner returns a new Scanner to read from r.
func NewScanner(rdr io.Reader) *Scanner {
sc := Scanner{bufio.NewScanner(rdr), 10}
return &sc
}
// Errors returned by Scanner
var (
ErrNoSuchElement = errors.New("gojus.Scanner: input is exhausted.")
)
// Scanner provides a similar interface to the java.util.Scaner's one.
type Scanner struct {
*bufio.Scanner
radix int
}
// Next scans and returns the next token of the input.
func (sc *Scanner) Next() (string, error) {
sc.Split(bufio.ScanWords)
if !sc.Scan() {
return "", ErrNoSuchElement
}
return sc.Text(), nil
}
// UseRadix sets the scanner's default radix to the specified one.
func (sc *Scanner) UseRadix(radix int) *Scanner {
sc.radix = radix
return sc
}
func (sc *Scanner) nextLong() (int64, error) {
t, e := sc.Next()
if e != nil {
return 0, e
}
return strconv.ParseInt(t, sc.radix, 64)
}
// NextLong scans and returns the next token of the input as an int64.
func (sc *Scanner) NextLong() (int64, error) {
return sc.nextLong()
}
// NextDouble scans and returns the next token of the input as a float64
func (sc *Scanner) NextDouble() (float64, error) {
t, e := sc.Next()
if e != nil {
return 0, e
}
return strconv.ParseFloat(t, 64)
}
// NextLine advances this scanner past the current line and returns
// the input that was skipped.
func (sc *Scanner) NextLine() (string, error) {
sc.Split(bufio.ScanLines)
if !sc.Scan() {
return "", ErrNoSuchElement
}
return sc.Text(), nil
}
////////////////////////////////////////////////////////////////
//
// int/int64 utilities
//
func minInt(a, b int) int {
if b < a {
return b
}
return a
}
func maxInt(a, b int) int {
if b > a {
return b
}
return a
}
func absInt(x int) int {
if x < 0 {
return -x
}
return x
}
func minInt64(a, b int64) int64 {
if b < a {
return b
}
return a
}
func maxInt64(a, b int64) int64 {
if b > a {
return b
}
return a
}
func absInt64(x int64) int64 {
if x < 0 {
return -x
}
return x
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment