-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
from: http://www.reddit.com/r/golang/comments/2s1zn1/how_can_i_improve_the_performance_of_my_xxd_port/
- Loading branch information
0 parents
commit ff11c6f
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"unicode/utf8" | ||
) | ||
|
||
const ( | ||
byteOffsetInit = 8 | ||
charOffsetInt = 39 | ||
line_length = 50 | ||
) | ||
|
||
func main() { | ||
line_offset := 0 | ||
|
||
if len(os.Args) != 2 { | ||
fmt.Fprintf(os.Stderr, "Usage: %s [file]\n", os.Args[0]) | ||
os.Exit(1) | ||
} | ||
|
||
f, err := os.Open(os.Args[1]) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
|
||
r := bufio.NewReader(f) | ||
buf := make([]byte, 16) | ||
for { | ||
n, err := r.Read(buf) | ||
if n == 0 || err == io.EOF { | ||
break | ||
} | ||
|
||
// Line offset | ||
fmt.Printf("%06x0: ", line_offset) | ||
line_offset++ | ||
|
||
// Hex values | ||
for i := 0; i < n; i++ { | ||
fmt.Printf("%02x", buf[i]) | ||
|
||
if i%2 == 1 { | ||
fmt.Print(" ") | ||
} | ||
} | ||
if n < len(buf) { | ||
for i := n; i < len(buf); i++ { | ||
fmt.Printf(" ") | ||
if i%2 == 1 { | ||
fmt.Print(" ") | ||
} | ||
} | ||
} | ||
|
||
fmt.Printf(" ") | ||
|
||
// Character values | ||
b := buf[:n] | ||
for len(b) > 0 { | ||
r, size := utf8.DecodeRune(b) | ||
|
||
if int(r) > 0x1f && int(r) < 0x7f { | ||
fmt.Printf("%v", string(r)) | ||
} else { | ||
fmt.Printf(".") | ||
} | ||
b = b[size:] | ||
} | ||
|
||
fmt.Printf("\n") | ||
} | ||
} |
ff11c6f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, but it looks a bit slow. ☺