9fans.net/go@v0.0.7/cmd/devdraw/winsize.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"9fans.net/go/draw"
     8  )
     9  
    10  func parsewinsize(s string, r *draw.Rectangle, havemin *bool) error {
    11  	os := s
    12  	isdigit := func(c byte) bool { return '0' <= c && c <= '9' }
    13  	strtol := func(s string, sp *string, base int) int {
    14  		i := 0
    15  		for i < len(s) && isdigit(s[i]) {
    16  			i++
    17  		}
    18  		*sp = s[i:]
    19  		n, _ := strconv.ParseInt(s[:i], base, 0)
    20  		return int(n)
    21  	}
    22  
    23  	*havemin = false
    24  	*r = draw.Rect(0, 0, 0, 0)
    25  	var i, j, k, l int
    26  	var c byte
    27  	if s == "" || !isdigit(s[0]) {
    28  		goto oops
    29  	}
    30  	i = strtol(s, &s, 0)
    31  	if s[0] == 'x' {
    32  		s = s[1:]
    33  		if s == "" || !isdigit(s[0]) {
    34  			goto oops
    35  		}
    36  		j = strtol(s, &s, 0)
    37  		r.Max.X = i
    38  		r.Max.Y = j
    39  		if len(s) == 0 {
    40  			return nil
    41  		}
    42  		if s[0] != '@' {
    43  			goto oops
    44  		}
    45  
    46  		s = s[1:]
    47  		if s == "" || !isdigit(s[0]) {
    48  			goto oops
    49  		}
    50  		i = strtol(s, &s, 0)
    51  		if s[0] != ',' && s[0] != ' ' {
    52  			goto oops
    53  		}
    54  		s = s[1:]
    55  		if s == "" || !isdigit(s[0]) {
    56  			goto oops
    57  		}
    58  		j = strtol(s, &s, 0)
    59  		if s[0] != 0 {
    60  			goto oops
    61  		}
    62  		*r = r.Add(draw.Pt(i, j))
    63  		*havemin = true
    64  		return nil
    65  	}
    66  
    67  	c = s[0]
    68  	if c != ' ' && c != ',' {
    69  		goto oops
    70  	}
    71  	s = s[1:]
    72  	if len(s) == 0 || !isdigit(s[0]) {
    73  		goto oops
    74  	}
    75  	j = strtol(s, &s, 0)
    76  	if s[0] != c {
    77  		goto oops
    78  	}
    79  	s = s[1:]
    80  	if len(s) == 0 || !isdigit(s[0]) {
    81  		goto oops
    82  	}
    83  	k = strtol(s, &s, 0)
    84  	if s[0] != c {
    85  		goto oops
    86  	}
    87  	s = s[1:]
    88  	if len(s) == 0 || !isdigit(s[0]) {
    89  		goto oops
    90  	}
    91  	l = strtol(s, &s, 0)
    92  	if s[0] != 0 {
    93  		goto oops
    94  	}
    95  	*r = draw.Rect(i, j, k, l)
    96  	*havemin = true
    97  	return nil
    98  
    99  oops:
   100  	return fmt.Errorf("bad syntax in window size '%s'", os)
   101  }