<commit_before>// Copyright 2016 Red Hat, Inc, and individual contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

type TokenizeError string

func (e TokenizeError) Error() string {
	return string(e)
}

func tokenize(s string) ([]string, error) {
	inQuote, escape := false, false
	var curQuote rune
	tokens := []string{}
	curToken := []rune{}

	for _, r := range s {
		appendEscape, appendRune, appendToken := false, true, false

		switch r {
		case '\\':
			appendRune = escape
			escape = !escape

		case '\'', '"':
			appendRune = false
			switch {
			case escape:
				appendEscape = !(inQuote && curQuote == r)
				appendRune = true

			case inQuote:
				inQuote = curQuote != r

			default:
				inQuote = true
				curQuote = r
			}
			escape = false

		case ' ', '\t':
			appendRune = escape
			if inQuote {
				appendEscape = escape
				appendRune = true
			} else {
				appendToken = !escape && len(curToken) > 0
			}
			escape = false

		default:
			appendEscape = escape
			escape = false
		}

		if appendEscape {
			curToken = append(curToken, '\\')
		}

		if appendRune {
			curToken = append(curToken, r)
		}

		if appendToken {
			tokens = append(tokens, string(curToken))
			curToken = []rune{}
		}
	}

	if inQuote {
		return tokens, TokenizeError("Unexpected end of input; did you forget to close a quote?")
	}

	if len(curToken) > 0 {
		tokens = append(tokens, string(curToken))
	}

	return tokens, nil
}<commit_msg>Less whitespace, just for @jcrossley3<commit_after>// Copyright 2016 Red Hat, Inc, and individual contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

type TokenizeError string

func (e TokenizeError) Error() string {
	return string(e)
}

func tokenize(s string) ([]string, error) {
	inQuote, escape := false, false
	var curQuote rune
	tokens := []string{}
	curToken := []rune{}

	for _, r := range s {
		appendEscape, appendRune, appendToken := false, true, false

		switch r {
		case '\\':
			appendRune = escape
			escape = !escape
		case '\'', '"':
			appendRune = false
			switch {
			case escape:
				appendEscape = !(inQuote && curQuote == r)
				appendRune = true

			case inQuote:
				inQuote = curQuote != r

			default:
				inQuote = true
				curQuote = r
			}
			escape = false
		case ' ', '\t':
			appendRune = escape
			if inQuote {
				appendEscape = escape
				appendRune = true
			} else {
				appendToken = !escape && len(curToken) > 0
			}
			escape = false
		default:
			appendEscape = escape
			escape = false
		}

		if appendEscape {
			curToken = append(curToken, '\\')
		}
		if appendRune {
			curToken = append(curToken, r)
		}
		if appendToken {
			tokens = append(tokens, string(curToken))
			curToken = []rune{}
		}
	}

	if inQuote {
		return tokens, TokenizeError("Unexpected end of input; did you forget to close a quote?")
	}
	if len(curToken) > 0 {
		tokens = append(tokens, string(curToken))
	}

	return tokens, nil
}
{'fraction_non_alphanumeric': 0.1095890410958904, 'fraction_numerical': 0.00532724505327245, 'mean_word_length': 3.0441025641025643, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4189276', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1203, 'n_words': 440}