Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2229)

Unified Diff: base/i18n/icu_encoding_detection.cc

Issue 4967001: FTP: improve character encoding detection in cases where ICU's first guess is wrong. (Closed)
Patch Set: now with tests Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/i18n/icu_encoding_detection.h ('k') | net/data/ftp/dir-listing-ls-20 » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/i18n/icu_encoding_detection.cc
diff --git a/base/i18n/icu_encoding_detection.cc b/base/i18n/icu_encoding_detection.cc
index 55785c51a86ff2eaaed2c4f1a62d7e919aa2750c..d579af280a7af7959ca6d48a3f125c87b98cb13c 100644
--- a/base/i18n/icu_encoding_detection.cc
+++ b/base/i18n/icu_encoding_detection.cc
@@ -9,8 +9,6 @@
namespace base {
-// TODO(jungshik): We can apply more heuristics here (e.g. using various hints
-// like TLD, the UI language/default encoding of a client, etc).
bool DetectEncoding(const std::string& text, std::string* encoding) {
if (IsStringASCII(text)) {
*encoding = std::string();
@@ -21,9 +19,6 @@ bool DetectEncoding(const std::string& text, std::string* encoding) {
UCharsetDetector* detector = ucsdet_open(&status);
ucsdet_setText(detector, text.data(), static_cast<int32_t>(text.length()),
&status);
- // TODO(jungshik): Should we check the quality of the match? A rather
- // arbitrary number is assigned by ICU and it's hard to come up with
- // a lower limit.
const UCharsetMatch* match = ucsdet_detect(detector, &status);
const char* detected_encoding = ucsdet_getName(match, &status);
ucsdet_close(detector);
@@ -35,4 +30,35 @@ bool DetectEncoding(const std::string& text, std::string* encoding) {
return true;
}
+bool DetectAllEncodings(const std::string& text,
cbentzel 2010/11/18 16:39:43 Is a bool needed here? Could empty encodings be su
+ std::vector<std::string>* encodings) {
+ UErrorCode status = U_ZERO_ERROR;
+ UCharsetDetector* detector = ucsdet_open(&status);
cbentzel 2010/11/18 16:39:43 Should you check for failure after this call? ht
+ ucsdet_setText(detector, text.data(), static_cast<int32_t>(text.length()),
cbentzel 2010/11/18 16:39:43 Should you check for failure after this call as we
+ &status);
+ int matches_count = 0;
+ const UCharsetMatch** matches = ucsdet_detectAll(detector,
+ &matches_count,
+ &status);
+ if (U_FAILURE(status)) {
+ ucsdet_close(detector);
+ return false;
+ }
+
+ encodings->clear();
+ for (int i = 0; i < matches_count; i++) {
+ UErrorCode get_name_status = U_ZERO_ERROR;
+ const char* encoding_name = ucsdet_getName(matches[i], &get_name_status);
+
+ // If we failed to get the encoding's name, ignore the error.
+ if (U_FAILURE(get_name_status))
+ continue;
+
+ encodings->push_back(encoding_name);
+ }
+
+ ucsdet_close(detector);
+ return !encodings->empty();
+}
+
} // namespace base
« no previous file with comments | « base/i18n/icu_encoding_detection.h ('k') | net/data/ftp/dir-listing-ls-20 » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698