Example #1
0
func charString(ch int) string {
	var s string
	switch ch {
	case -1:
		return `EOF`
	case '\a':
		s = `\a`
	case '\b':
		s = `\b`
	case '\f':
		s = `\f`
	case '\n':
		s = `\n`
	case '\r':
		s = `\r`
	case '\t':
		s = `\t`
	case '\v':
		s = `\v`
	case '\\':
		s = `\\`
	case '\'':
		s = `\'`
	default:
		s = string(ch)
	}
	return "'" + s + "' (U+" + strconv.Itob(ch, 16) + ")"
}
Example #2
0
func main() {
	if five_as_bool := strconv.Itob(5); five_as_bool {
		fmt.Printf("True\n")
	} else {
		fmt.Printf("False\n")
	}
}
Example #3
0
func charString(ch int) string {
	var s string;
	switch ch {
	case -1:
		return `EOF`;
	case '\a':
		s = `\a`;
	case '\b':
		s = `\b`;
	case '\f':
		s = `\f`;
	case '\n':
		s = `\n`;
	case '\r':
		s = `\r`;
	case '\t':
		s = `\t`;
	case '\v':
		s = `\v`;
	case '\\':
		s = `\\`;
	case '\'':
		s = `\'`;
	default:
		s = string(ch);
	}
	return "'" + s + "' (U+" + strconv.Itob(ch, 16) + ")";
}
Example #4
0
// The SQLite database interface returns keys "version",
// "sqlite3.sourceid", and "sqlite3.versionnumber"; the
// latter are specific to SQLite.
func version() (data map[string]string, error os.Error) {
	// TODO: fake client and server keys?
	data = make(map[string]string)
	data["version"] = sqlVersion()
	i := sqlVersionNumber()
	data["sqlite3.versionnumber"] = strconv.Itob(i, 10)
	data["sqlite3.sourceid"] = sqlSourceId()
	return
}
Example #5
0
func main() {
	sum := 0
	for i := 1; i < 1000000; i++ {
		if palidrome(strconv.Itoa(i)) &&
			palidrome(strconv.Itob(i, 2)) {
			sum += i
		}
	}
	fmt.Printf("%d\n", sum)
}
Example #6
0
func Euler36() string {
	sum := 0
	for i := 1; i < 1000000; i++ {
		if palidrome(strconv.Itoa(i)) &&
			palidrome(strconv.Itob(i, 2)) {
			sum += i
		}
	}
	return fmt.Sprint(sum)
}
Example #7
0
func TestFizzBuzz(t *testing.T) {
	var numbers = FizzBuzz()
	if len(numbers) != 100 {
		t.Errorf("100 items expected.")
	}
	var number int
	for i := 0; i < len(numbers); i++ {
		number = i + 1
		if number%5 == 0 && number%3 == 0 && !strings.Contains(numbers[i], "FizzBuzz") {
			t.Errorf("Expecting FizzBuzz for %d. Found %s.", number, numbers[i])
		} else if number%5 == 0 && !strings.Contains(numbers[i], "Buzz") {
			t.Errorf("Expecting Buzz for %d. Found %s.", number, numbers[i])
		} else if number%3 == 0 && !strings.Contains(numbers[i], "Fizz") {
			t.Errorf("Expecting Fizz for %d. Found %s.", number, numbers[i])
		} else if number%5 != 0 && number%3 != 0 && !strings.Contains(numbers[i], strconv.Itob(number, 10)) {
			t.Errorf("Expecting %s at %d. Found %s.", strconv.Itob(number, 10), i, numbers[i])
		}
	}
}
Example #8
0
func main() {
	sum := 0
	for i := 0; i < 1000000; i++ {
		if isPalindrome(strconv.Itoa(i)) {
			if isPalindrome(strconv.Itob(i, 2)) {
				sum += i
			}
		}
	}
	println(sum)
}
Example #9
0
func TestHexDecode(t *testing.T) {
	for i := 0; i < 0x200000; i += 101 /* coprime with 16 */ {
		s := strconv.Itob(i, 16)
		if got := int(hexDecode([]byte(s))); got != i {
			t.Errorf("%s: want %d but got %d", s, i, got)
		}
		s = strings.ToUpper(s)
		if got := int(hexDecode([]byte(s))); got != i {
			t.Errorf("%s: want %d but got %d", s, i, got)
		}
	}
}
Example #10
0
func escape(b *bytes.Buffer, r int, force bool) {
	if unicode.IsPrint(r) {
		if strings.IndexRune(meta, r) >= 0 || force {
			b.WriteRune('\\')
		}
		b.WriteRune(r)
		return
	}

	switch r {
	case '\a':
		b.WriteString(`\a`)
	case '\f':
		b.WriteString(`\f`)
	case '\n':
		b.WriteString(`\n`)
	case '\r':
		b.WriteString(`\r`)
	case '\t':
		b.WriteString(`\t`)
	case '\v':
		b.WriteString(`\v`)
	default:
		if r < 0x100 {
			b.WriteString(`\x`)
			s := strconv.Itob(r, 16)
			if len(s) == 1 {
				b.WriteRune('0')
			}
			b.WriteString(s)
			break
		}
		b.WriteString(`\x{`)
		b.WriteString(strconv.Itob(r, 16))
		b.WriteString(`}`)
	}
}
Example #11
0
func FizzBuzz() [100]string {
	numbers := [100]string{}
	fizzOrBuzzOrFizzBuzzOrNumber := ""
	for i := 1; i < 101; i++ {
		if i%5 == 0 && i%3 == 0 {
			fizzOrBuzzOrFizzBuzzOrNumber = "FizzBuzz"
		} else if i%3 == 0 {
			fizzOrBuzzOrFizzBuzzOrNumber = "Fizz"
		} else if i%5 == 0 {
			fizzOrBuzzOrFizzBuzzOrNumber = "Buzz"
		} else {
			fizzOrBuzzOrFizzBuzzOrNumber = strconv.Itob(i, 10)
		}
		numbers[i-1] = fizzOrBuzzOrFizzBuzzOrNumber
	}
	return numbers
}
Example #12
0
func (c chunkedWriter) Write(p []byte) (int, os.Error) {
	if c.responseErr != nil {
		return 0, c.responseErr
	}
	if len(p) == 0 {
		return 0, nil
	}
	_, c.responseErr = io.WriteString(c.netConn, strconv.Itob(len(p), 16)+"\r\n")
	if c.responseErr != nil {
		return 0, c.responseErr
	}
	var n int
	n, c.responseErr = c.netConn.Write(p)
	if c.responseErr != nil {
		return n, c.responseErr
	}
	_, c.responseErr = io.WriteString(c.netConn, "\r\n")
	return 0, c.responseErr
}
Example #13
0
func (f *File) GoSet(which Field) {
	if which == 0 {
		return
	} // Nothing to set!
	ago.Import("github.com/droundy/goadmin/file")
	ago.Declare("var changed_files = make(map[string]bool)")
	ago.Declare("var file_changed = false")
	ago.Declare("var this_file_changed = false")
	ago.Declare("var myf file.File")
	code := "myf = file.File{file.StatData{" + strconv.Quote(f.Name) + ","
	code += fmt.Sprint(f.Uid, ",", f.Gid, ", 0", strconv.Itob(int(f.Perms), 8), ", file.IsFile},")
	if Contents&which != 0 {
		code += strconv.Quote(f.Contents) + "}"
	} else {
		code += `""}`
	}
	code += fmt.Sprint("\n\tthis_file_changed, e = myf.Set(", which, ")")
	code += fmt.Sprint("\n\tif e != nil { fmt.Println(`Trouble setting", f.Name, ":`,e); return }")
	code += fmt.Sprint("\n\tif this_file_changed { changed_files[`", f.Name, "`] = true }")
	code += fmt.Sprint("\n\tfile_changed = file_changed || this_file_changed")
	ago.Code(code)
}
Example #14
0
// Write the contents of data as one chunk to Wire.
// NOTE: Note that the corresponding chunk-writing procedure in Conn.Write has
// a bug since it does not check for success of io.WriteString
func (cw *chunkedWriter) Write(data []byte) (n int, err error) {

	// Don't send 0-length data. It looks like EOF for chunked encoding.
	if len(data) == 0 {
		return 0, nil
	}

	head := strconv.Itob(len(data), 16) + "\r\n"

	if _, err = io.WriteString(cw.Wire, head); err != nil {
		return 0, err
	}
	if n, err = cw.Wire.Write(data); err != nil {
		return
	}
	if n != len(data) {
		err = io.ErrShortWrite
		return
	}
	_, err = io.WriteString(cw.Wire, "\r\n")

	return
}
Example #15
0
func (f *Directory) GoSet(which Field) {
	if which == 0 {
		return
	} // Nothing to set!
	ago.Import("github.com/droundy/goadmin/file")
	ago.Declare("var changed_files = make(map[string]bool)")
	ago.Declare("var file_changed = false")
	ago.Declare("var this_file_changed = false")
	ago.Declare("var myd file.Directory")
	code := "myd = file.Directory{file.StatData{" + strconv.Quote(f.Name) + ","
	code += fmt.Sprint(f.Uid, ",", f.Gid, ", 0", strconv.Itob(int(f.Perms), 8), ", file.IsFile},")
	code += `map[string]file.StatEntry{}}`
	code += fmt.Sprint("\n\tthis_file_changed, e = myd.Set(", which, ")")
	code += fmt.Sprint("\n\tif e != nil { return }")
	code += fmt.Sprint("\n\tif this_file_changed { changed_files[`", f.Name, "`] = true }")
	code += fmt.Sprint("\n\tfile_changed = file_changed || this_file_changed")
	ago.Code(code)
	if Contents&which != 0 {
		for _, x := range f.Contents {
			x.GoSet(which)
		}
	}
}
Example #16
0
}

func TestHandshakeServerAES(t *testing.T) {
	aesConfig := new(Config)
	*aesConfig = *testConfig
	aesConfig.CipherSuites = []uint16{TLS_RSA_WITH_AES_128_CBC_SHA}
	testServerScript(t, "AES", aesServerScript, aesConfig)
}

func TestHandshakeServerSSLv3(t *testing.T) {
	testServerScript(t, "SSLv3", sslv3ServerScript, testConfig)
}

var serve = flag.Bool("serve", false, "run a TLS server on :10443")
var testCipherSuites = flag.String("ciphersuites",
	"0x"+strconv.Itob(int(TLS_RSA_WITH_RC4_128_SHA), 16),
	"cipher suites to accept in serving mode")

func TestRunServer(t *testing.T) {
	if !*serve {
		return
	}

	suites := strings.Split(*testCipherSuites, ",")
	testConfig.CipherSuites = make([]uint16, len(suites))
	for i := range suites {
		suite, err := strconv.Btoui64(suites[i], 0)
		if err != nil {
			panic(err)
		}
		testConfig.CipherSuites[i] = uint16(suite)
func createClient(wg *sync.WaitGroup) {
	destination := "127.0.0.1:9988"
	clientId := <-idChannel
	loggerChannel := make(chan string)
	loggerConfirmationChannel := make(chan bool)
	go func() {
		wg.Add(1)
		defer wg.Done()
		var fd *os.File

		var err os.Error
		if *loggingOn {
			file := "user" + strconv.Itob(clientId, 10) + ".log"
			fd, err = os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_TRUNC|os.O_SYNC, 0644)
			if err != nil {
				println(fmt.Sprintf("%v : ", file) + errorString("", err))
				return
			}
			defer fd.Close()
		}
		for str := range loggerChannel {
			if *loggingOn {
				fmt.Fprintf(fd, str)
			} else if *masterLoggingOn {
				masterMessage := "user" + strconv.Itob(clientId, 10) + " : " + str
				masterLoggerChannel <- masterMessage
				<-masterLoggerConfirmationChannel
			}
			loggerConfirmationChannel <- true
		}

	}()

	cn, netErr := net.Dial("tcp", destination)
	if netErr != nil {
		loggerChannel <- fmt.Sprintf("connection error for client %d : ", clientId) + errorString("", netErr)
		<-loggerConfirmationChannel
		return
	}

	clientsender := func() {
		wg.Add(1)
		defer wg.Done()
		ns := int64(rand.Intn(100000))
		triggerChannel := time.Tick(ns)
		_, nCmdChannel := randomValidFullCommand()
		readerChannel := Read(&cn)
		for {
			<-triggerChannel
			select {
			case reply := <-readerChannel:
				loggerChannel <- reply
				<-loggerConfirmationChannel
			case ncmd := <-nCmdChannel:
				loggerChannel <- ncmd
				<-loggerConfirmationChannel
				cn.Write([]byte(ncmd))
			}
			if closed(nCmdChannel) || closed(readerChannel) {
				close(loggerChannel)
				close(loggerConfirmationChannel)
				break
			}
		}
	}
	go clientsender()
}
Example #18
0
// newArticleID returns a random integer, expressed as a base-36 string.
// Used to generate new keys for articles.
func newArticleID() string {
	r := rand.New(rand.NewSource(time.Nanoseconds()))
	return strconv.Itob(r.Int(), 36)
}
Example #19
0
func base36(i int) string {
	return strconv.Itob(i, 36)
}