Skip to content

Instantly share code, notes, and snippets.

@mmcdole
Created December 19, 2015 06:29
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 mmcdole/e82cf881e5a6e1f0c894 to your computer and use it in GitHub Desktop.
Save mmcdole/e82cf881e5a6e1f0c894 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"os"
"regexp"
)
var (
literalSlash = regexp.MustCompile(`\\\\`)
escapedQuote = regexp.MustCompile(`\\"`)
hexLetter = regexp.MustCompile(`\\x[a-f0-9]{2}`)
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
codeChars := 0
valueChars := 0
for scanner.Scan() {
line := scanner.Text()
codeChars += len(line)
// Surrounding Quotes
line = line[1 : len(line)-1]
// Slash Literals
line = literalSlash.ReplaceAllString(line, "\\")
// Escaped Quotes
line = escapedQuote.ReplaceAllString(line, "\"")
// Hex Letter
line = hexLetter.ReplaceAllString(line, "!")
valueChars += len(line)
}
fmt.Printf("codeChars: %d - valueChars: %d = %d\n", codeChars, valueChars, codeChars-valueChars)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment