-
Notifications
You must be signed in to change notification settings - Fork 6.1k
types: print argument as hex literal if non-printable #65384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ import ( | |
| "strconv" | ||
| "strings" | ||
| "time" | ||
| "unicode" | ||
| "unicode/utf8" | ||
| "unsafe" | ||
|
|
||
|
|
@@ -2420,8 +2421,34 @@ func SortDatums(ctx Context, datums []Datum) error { | |
| return err | ||
| } | ||
|
|
||
| // Check if a string is considered printable | ||
| // | ||
| // Checks | ||
| // 1. Must be valid UTF-8 | ||
| // 2. Must not contain control characters like NUL (0x0) and backspace (0x8) | ||
| func isPrintable(s string) bool { | ||
| if !utf8.ValidString(s) { | ||
| return false | ||
| } | ||
| for _, r := range s { | ||
| if unicode.IsControl(r) { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| // DatumsToString converts several datums to formatted string. | ||
| func DatumsToString(datums []Datum, handleSpecialValue bool) (string, error) { | ||
| return datumsToString(datums, handleSpecialValue, false) | ||
| } | ||
|
|
||
| // DatumsToStringSmart is like DatumsToString, but with smart detection of non-printable data | ||
| func DatumsToStringSmart(datums []Datum, handleSpecialValue bool) (string, error) { | ||
| return datumsToString(datums, handleSpecialValue, true) | ||
| } | ||
|
|
||
| func datumsToString(datums []Datum, handleSpecialValue bool, binaryAsHex bool) (string, error) { | ||
| n := len(datums) | ||
| builder := &strings.Builder{} | ||
| builder.Grow(8 * n) | ||
|
|
@@ -2456,9 +2483,14 @@ func DatumsToString(datums []Datum, handleSpecialValue bool) (string, error) { | |
| str = str[:logDatumLen] | ||
| } | ||
| if datum.Kind() == KindString { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the type of binary is string, not
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is due to the response that we send for the prepare statement. We set the collation to utf8mb4_bin with no binary flag. We might be able to improve this by changing the response we send for the prepare call. |
||
| builder.WriteString(`"`) | ||
| builder.WriteString(str) | ||
| builder.WriteString(`"`) | ||
| if !binaryAsHex || isPrintable(str) { | ||
| builder.WriteString(`"`) | ||
| builder.WriteString(str) | ||
| builder.WriteString(`"`) | ||
| } else { | ||
| // Print as hex-literal instead | ||
| fmt.Fprintf(builder, "0x%X", str) | ||
| } | ||
| } else { | ||
| builder.WriteString(str) | ||
| } | ||
|
|
@@ -2482,6 +2514,15 @@ func DatumsToStrNoErr(datums []Datum) string { | |
| return str | ||
| } | ||
|
|
||
| // DatumsToStrNoErrSmart converts some datums to a formatted string. | ||
| // If an error occurs, it will print a log instead of returning an error. | ||
| // It also enables detection of non-pritable arguments | ||
| func DatumsToStrNoErrSmart(datums []Datum) string { | ||
| str, err := DatumsToStringSmart(datums, true) | ||
| terror.Log(errors.Trace(err)) | ||
| return str | ||
| } | ||
|
|
||
| // CloneRow deep copies a Datum slice. | ||
| func CloneRow(dr []Datum) []Datum { | ||
| c := make([]Datum, len(dr)) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.