<commit_before>// Copyright 2014 <NAME>. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"os"

	"github.com/gorilla/websocket"
	"golang.org/x/crypto/ssh/terminal"
)

func speculoHandler(w http.ResponseWriter, r *http.Request) {
	ws, err := websocket.Upgrade(w, r, nil, 1024, 1024)
	if _, ok := err.(websocket.HandshakeError); ok {
		http.Error(w, "Not a websocket handshake", 400)
		return
	} else if err != nil {
		log.Fatal(err)
		return
	}

	oldState, err := terminal.MakeRaw(0)
	if err != nil {
		panic(err)
	}
	defer terminal.Restore(0, oldState)
	term := terminal.NewTerminal(os.Stdin, "> ")

	term.AutoCompleteCallback = func(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
		if key == '\x03' {
			fmt.Println()
			if oldState != nil {
				terminal.Restore(0, oldState)
			}
			os.Exit(0)
		}
		return "", 0, false
	}

	go func() {
		for {
			_, r, err := ws.ReadMessage() // messageType is ignored
			if err != nil {
				log.Fatal(err)
			}
			term.Write(r)
			term.Write([]byte("\n"))
		}
	}()

	for {
		text, err := term.ReadLine()
		if err != nil {
			// Ctrl-D is EOF. So, quit without printing stacktrace.
			if err == io.EOF {
				fmt.Println()
				if oldState != nil {
					terminal.Restore(0, oldState)
				}
				os.Exit(0)
			}
			panic(err)
		}
		inputcommand := []byte(text)
		if err := ws.WriteMessage(1, inputcommand); err != nil {
			log.Println(err)
		}
	}
}

func main() {
	path, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}

	http.Handle("/", http.FileServer(http.Dir(path)))
	http.HandleFunc("/repl", speculoHandler)<commit_msg>reduce allocation in hot path<commit_after>// Copyright 2014 <NAME>. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"os"

	"github.com/gorilla/websocket"
	"golang.org/x/crypto/ssh/terminal"
)

func speculoHandler(w http.ResponseWriter, r *http.Request) {
	ws, err := websocket.Upgrade(w, r, nil, 1024, 1024)
	if _, ok := err.(websocket.HandshakeError); ok {
		http.Error(w, "Not a websocket handshake", 400)
		return
	} else if err != nil {
		log.Fatal(err)
		return
	}

	oldState, err := terminal.MakeRaw(0)
	if err != nil {
		panic(err)
	}
	defer terminal.Restore(0, oldState)
	term := terminal.NewTerminal(os.Stdin, "> ")

	term.AutoCompleteCallback = func(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
		if key == '\x03' { // Ctrl-C keycode is \x03
			fmt.Println()
			if oldState != nil {
				terminal.Restore(0, oldState)
			}
			os.Exit(0)
		}
		return "", 0, false
	}

	go func() {
		newline := []byte("\n")
		for {
			_, r, err := ws.ReadMessage() // messageType is ignored
			if err != nil {
				log.Fatal(err)
			}
			term.Write(r)
			term.Write(newline)
		}
	}()

	for {
		text, err := term.ReadLine()
		if err != nil {
			// Ctrl-D is EOF. So, quit without printing stacktrace.
			if err == io.EOF {
				fmt.Println()
				if oldState != nil {
					terminal.Restore(0, oldState)
				}
				os.Exit(0)
			}
			panic(err)
		}
		inputcommand := []byte(text)
		if err := ws.WriteMessage(1, inputcommand); err != nil {
			log.Println(err)
		}
	}
}

func main() {
	path, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}

	http.Handle("/", http.FileServer(http.Dir(path)))
	http.HandleFunc("/repl", speculoHandler)
{'fraction_non_alphanumeric': 0.15965665236051502, 'fraction_numerical': 0.014878397711015737, 'mean_word_length': 2.9592298980747453, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2034073', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1314, 'n_words': 341}