-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Convert files to utf-8 for indexing #7814
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
Merged
lunny
merged 12 commits into
go-gitea:master
from
guillep2k:repo-idx-normalize-encoding
Aug 15, 2019
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cdc587d
Convert files to utf-8 for indexing
guillep2k e44b94f
Move utf8 functions to modules/base
guillep2k 152dd1a
Bump repoIndexerLatestVersion to 3
guillep2k b1102c4
Merge branch 'master' into repo-idx-normalize-encoding
lunny 8bf8166
Merge branch 'master' of github.com:go-gitea/gitea into repo-idx-norm…
guillep2k 504d18e
Merge branch 'repo-idx-normalize-encoding' of github.com:guillep2k/gi…
guillep2k b1d3729
Add tests for base/encoding.go
guillep2k 84db7d2
Changes to pass gosimple
guillep2k 654e9ea
Merge branch 'master' of github.com:go-gitea/gitea into repo-idx-norm…
guillep2k 2d6865f
Move UTF8 funcs into new modules/charset package
guillep2k 2033911
Merge branch 'master' into repo-idx-normalize-encoding
lafriks e48344a
Merge branch 'master' into repo-idx-normalize-encoding
lafriks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,99 @@ | ||
// Copyright 2014 The Gogs Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package base | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.org/x/net/html/charset" | ||
"golang.org/x/text/transform" | ||
) | ||
|
||
// ToUTF8WithErr converts content to UTF8 encoding | ||
func ToUTF8WithErr(content []byte) (string, error) { | ||
charsetLabel, err := DetectEncoding(content) | ||
if err != nil { | ||
return "", err | ||
} else if charsetLabel == "UTF-8" { | ||
return string(RemoveBOMIfPresent(content)), nil | ||
} | ||
|
||
encoding, _ := charset.Lookup(charsetLabel) | ||
if encoding == nil { | ||
return string(content), fmt.Errorf("Unknown encoding: %s", charsetLabel) | ||
} | ||
|
||
// If there is an error, we concatenate the nicely decoded part and the | ||
// original left over. This way we won't lose data. | ||
result, n, err := transform.Bytes(encoding.NewDecoder(), content) | ||
if err != nil { | ||
result = append(result, content[n:]...) | ||
} | ||
|
||
result = RemoveBOMIfPresent(result) | ||
|
||
return string(result), err | ||
} | ||
|
||
// ToUTF8WithFallback detects the encoding of content and coverts to UTF-8 if possible | ||
func ToUTF8WithFallback(content []byte) []byte { | ||
charsetLabel, err := DetectEncoding(content) | ||
if err != nil || charsetLabel == "UTF-8" { | ||
return RemoveBOMIfPresent(content) | ||
} | ||
|
||
encoding, _ := charset.Lookup(charsetLabel) | ||
if encoding == nil { | ||
return content | ||
} | ||
|
||
// If there is an error, we concatenate the nicely decoded part and the | ||
// original left over. This way we won't lose data. | ||
result, n, err := transform.Bytes(encoding.NewDecoder(), content) | ||
if err != nil { | ||
return append(result, content[n:]...) | ||
} | ||
|
||
return RemoveBOMIfPresent(result) | ||
} | ||
|
||
// ToUTF8 converts content to UTF8 encoding and ignore error | ||
func ToUTF8(content string) string { | ||
res, _ := ToUTF8WithErr([]byte(content)) | ||
return res | ||
} | ||
|
||
// ToUTF8DropErrors makes sure the return string is valid utf-8; attempts conversion if possible | ||
func ToUTF8DropErrors(content []byte) []byte { | ||
charsetLabel, err := DetectEncoding(content) | ||
if err != nil || charsetLabel == "UTF-8" { | ||
return RemoveBOMIfPresent(content) | ||
} | ||
|
||
encoding, _ := charset.Lookup(charsetLabel) | ||
if encoding == nil { | ||
return content | ||
} | ||
|
||
// We ignore any non-decodable parts from the file. | ||
// Some parts might be lost | ||
var decoded []byte | ||
decoder := encoding.NewDecoder() | ||
idx := 0 | ||
for { | ||
result, n, err := transform.Bytes(decoder, content[idx:]) | ||
decoded = append(decoded, result...) | ||
if err == nil { | ||
break | ||
} | ||
decoded = append(decoded, ' ') | ||
idx = idx + n + 1 | ||
if idx >= len(content) { | ||
break | ||
} | ||
} | ||
|
||
return RemoveBOMIfPresent(decoded) | ||
} |
This file contains hidden or 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,145 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package base | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestToUTF8WithErr(t *testing.T) { | ||
var res string | ||
var err error | ||
|
||
res, err = ToUTF8WithErr([]byte{0x41, 0x42, 0x43}) | ||
assert.Equal(t, "ABC", res) | ||
assert.NoError(t, err) | ||
|
||
res, err = ToUTF8WithErr([]byte{0xc3, 0xa1, 0xc3, 0xa9, 0xc3, 0xad, 0xc3, 0xb3, 0xc3, 0xba}) | ||
assert.Equal(t, "áéíóú", res) | ||
assert.NoError(t, err) | ||
|
||
res, err = ToUTF8WithErr([]byte{0xef, 0xbb, 0xbf, 0xc3, 0xa1, 0xc3, 0xa9, 0xc3, 0xad, 0xc3, 0xb3, 0xc3, 0xba}) | ||
assert.Equal(t, "áéíóú", res) | ||
assert.NoError(t, err) | ||
|
||
// This test FAILS | ||
res, err = ToUTF8WithErr([]byte{0x48, 0x6F, 0x6C, 0x61, 0x2C, 0x20, 0x61, 0x73, 0xED, 0x20, 0x63, 0xF3, 0x6D, 0x6F, 0x20, 0xF1, 0x6F, 0x73}) | ||
assert.Equal(t, "Hola, así cómo ños", res) | ||
assert.NoError(t, err) | ||
|
||
res, err = ToUTF8WithErr([]byte{0x48, 0x6F, 0x6C, 0x61, 0x2C, 0x20, 0x61, 0x73, 0xED, 0x20, 0x63, 0xF3, 0x6D, 0x6F, 0x20, 0x07, 0xA4, 0x6F, 0x73}) | ||
// Do not fail for differences in invalid cases, as the library might change the conversion criteria for those | ||
assert.Regexp(t, "^Hola, así cómo", res) | ||
assert.NoError(t, err) | ||
|
||
res, err = ToUTF8WithErr([]byte{0x48, 0x6F, 0x6C, 0x61, 0x2C, 0x20, 0x61, 0x73, 0xED, 0x20, 0x63, 0xF3, 0x6D, 0x6F, 0x20, 0x81, 0xA4, 0x6F, 0x73}) | ||
// Do not fail for differences in invalid cases, as the library might change the conversion criteria for those | ||
assert.Regexp(t, "^Hola, así cómo", res) | ||
assert.NoError(t, err) | ||
|
||
// Japanese (Shift-JIS) | ||
res, err = ToUTF8WithErr([]byte{0x93, 0xFA, 0x91, 0xAE, 0x94, 0xE9, 0x82, 0xBC, 0x82, 0xB5, 0x82, 0xBF, 0x82, 0xE3, 0x81, 0x42}) | ||
assert.Equal(t, "日属秘ぞしちゅ。", res) | ||
assert.NoError(t, err) | ||
|
||
res, err = ToUTF8WithErr([]byte{0x00, 0x00, 0x00, 0x00}) | ||
assert.Equal(t, "\x00\x00\x00\x00", res) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestToUTF8WithFallback(t *testing.T) { | ||
res := ToUTF8WithFallback([]byte{0x41, 0x42, 0x43}) | ||
assert.Equal(t, []byte("ABC"), res) | ||
|
||
res = ToUTF8WithFallback([]byte{0xc3, 0xa1, 0xc3, 0xa9, 0xc3, 0xad, 0xc3, 0xb3, 0xc3, 0xba}) | ||
assert.Equal(t, []byte("áéíóú"), res) | ||
|
||
res = ToUTF8WithFallback([]byte{0xef, 0xbb, 0xbf, 0xc3, 0xa1, 0xc3, 0xa9, 0xc3, 0xad, 0xc3, 0xb3, 0xc3, 0xba}) | ||
assert.Equal(t, []byte("áéíóú"), res) | ||
|
||
res = ToUTF8WithFallback([]byte{0x48, 0x6F, 0x6C, 0x61, 0x2C, 0x20, 0x61, 0x73, 0xED, 0x20, 0x63, 0xF3, 0x6D, 0x6F, 0x20, 0xF1, 0x6F, 0x73}) | ||
assert.Equal(t, []byte("Hola, así cómo ños"), res) | ||
|
||
minmatch := []byte("Hola, así cómo ") | ||
|
||
res = ToUTF8WithFallback([]byte{0x48, 0x6F, 0x6C, 0x61, 0x2C, 0x20, 0x61, 0x73, 0xED, 0x20, 0x63, 0xF3, 0x6D, 0x6F, 0x20, 0x07, 0xA4, 0x6F, 0x73}) | ||
// Do not fail for differences in invalid cases, as the library might change the conversion criteria for those | ||
assert.Equal(t, minmatch, res[0:len(minmatch)]) | ||
|
||
res = ToUTF8WithFallback([]byte{0x48, 0x6F, 0x6C, 0x61, 0x2C, 0x20, 0x61, 0x73, 0xED, 0x20, 0x63, 0xF3, 0x6D, 0x6F, 0x20, 0x81, 0xA4, 0x6F, 0x73}) | ||
// Do not fail for differences in invalid cases, as the library might change the conversion criteria for those | ||
assert.Equal(t, minmatch, res[0:len(minmatch)]) | ||
|
||
// Japanese (Shift-JIS) | ||
res = ToUTF8WithFallback([]byte{0x93, 0xFA, 0x91, 0xAE, 0x94, 0xE9, 0x82, 0xBC, 0x82, 0xB5, 0x82, 0xBF, 0x82, 0xE3, 0x81, 0x42}) | ||
assert.Equal(t, []byte("日属秘ぞしちゅ。"), res) | ||
|
||
res = ToUTF8WithFallback([]byte{0x00, 0x00, 0x00, 0x00}) | ||
assert.Equal(t, []byte{0x00, 0x00, 0x00, 0x00}, res) | ||
} | ||
|
||
func TestToUTF8(t *testing.T) { | ||
res := ToUTF8("ABC") | ||
assert.Equal(t, "ABC", res) | ||
|
||
res = ToUTF8("áéíóú") | ||
assert.Equal(t, "áéíóú", res) | ||
|
||
// With utf-8 BOM | ||
res = ToUTF8("\ufeffáéíóú") | ||
assert.Equal(t, "áéíóú", res) | ||
|
||
res = ToUTF8("Hola, así cómo ños") | ||
assert.Equal(t, "Hola, así cómo ños", res) | ||
|
||
res = ToUTF8("Hola, así cómo \x07ños") | ||
// Do not fail for differences in invalid cases, as the library might change the conversion criteria for those | ||
assert.Regexp(t, "^Hola, así cómo", res) | ||
|
||
// This test FAILS | ||
// res = ToUTF8("Hola, así cómo \x81ños") | ||
// Do not fail for differences in invalid cases, as the library might change the conversion criteria for those | ||
// assert.Regexp(t, "^Hola, así cómo", res) | ||
|
||
// Japanese (Shift-JIS) | ||
res = ToUTF8("\x93\xFA\x91\xAE\x94\xE9\x82\xBC\x82\xB5\x82\xBF\x82\xE3\x81\x42") | ||
assert.Equal(t, "日属秘ぞしちゅ。", res) | ||
|
||
res = ToUTF8("\x00\x00\x00\x00") | ||
assert.Equal(t, "\x00\x00\x00\x00", res) | ||
} | ||
|
||
func TestToUTF8DropErrors(t *testing.T) { | ||
res := ToUTF8DropErrors([]byte{0x41, 0x42, 0x43}) | ||
assert.Equal(t, []byte("ABC"), res) | ||
|
||
res = ToUTF8DropErrors([]byte{0xc3, 0xa1, 0xc3, 0xa9, 0xc3, 0xad, 0xc3, 0xb3, 0xc3, 0xba}) | ||
assert.Equal(t, []byte("áéíóú"), res) | ||
|
||
res = ToUTF8DropErrors([]byte{0xef, 0xbb, 0xbf, 0xc3, 0xa1, 0xc3, 0xa9, 0xc3, 0xad, 0xc3, 0xb3, 0xc3, 0xba}) | ||
assert.Equal(t, []byte("áéíóú"), res) | ||
|
||
res = ToUTF8DropErrors([]byte{0x48, 0x6F, 0x6C, 0x61, 0x2C, 0x20, 0x61, 0x73, 0xED, 0x20, 0x63, 0xF3, 0x6D, 0x6F, 0x20, 0xF1, 0x6F, 0x73}) | ||
assert.Equal(t, []byte("Hola, así cómo ños"), res) | ||
|
||
minmatch := []byte("Hola, así cómo ") | ||
|
||
res = ToUTF8DropErrors([]byte{0x48, 0x6F, 0x6C, 0x61, 0x2C, 0x20, 0x61, 0x73, 0xED, 0x20, 0x63, 0xF3, 0x6D, 0x6F, 0x20, 0x07, 0xA4, 0x6F, 0x73}) | ||
// Do not fail for differences in invalid cases, as the library might change the conversion criteria for those | ||
assert.Equal(t, minmatch, res[0:len(minmatch)]) | ||
|
||
res = ToUTF8DropErrors([]byte{0x48, 0x6F, 0x6C, 0x61, 0x2C, 0x20, 0x61, 0x73, 0xED, 0x20, 0x63, 0xF3, 0x6D, 0x6F, 0x20, 0x81, 0xA4, 0x6F, 0x73}) | ||
// Do not fail for differences in invalid cases, as the library might change the conversion criteria for those | ||
assert.Equal(t, minmatch, res[0:len(minmatch)]) | ||
|
||
// Japanese (Shift-JIS) | ||
res = ToUTF8DropErrors([]byte{0x93, 0xFA, 0x91, 0xAE, 0x94, 0xE9, 0x82, 0xBC, 0x82, 0xB5, 0x82, 0xBF, 0x82, 0xE3, 0x81, 0x42}) | ||
assert.Equal(t, []byte("日属秘ぞしちゅ。"), res) | ||
|
||
res = ToUTF8DropErrors([]byte{0x00, 0x00, 0x00, 0x00}) | ||
assert.Equal(t, []byte{0x00, 0x00, 0x00, 0x00}, res) | ||
} |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.