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

Side by Side Diff: base/i18n/icu_encoding_detection.cc

Issue 6718043: FTP: Multiple fixes for localized directory listings: (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: update copyright year for the bot Created 9 years, 8 months 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/i18n/icu_encoding_detection.h ('k') | net/data/ftp/dir-listing-ls-25 » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/i18n/icu_encoding_detection.h" 5 #include "base/i18n/icu_encoding_detection.h"
6 6
7 #include <set>
8
7 #include "base/string_util.h" 9 #include "base/string_util.h"
8 #include "unicode/ucsdet.h" 10 #include "unicode/ucsdet.h"
9 11
10 namespace base { 12 namespace base {
11 13
12 bool DetectEncoding(const std::string& text, std::string* encoding) { 14 bool DetectEncoding(const std::string& text, std::string* encoding) {
13 if (IsStringASCII(text)) { 15 if (IsStringASCII(text)) {
14 *encoding = std::string(); 16 *encoding = std::string();
15 return true; 17 return true;
16 } 18 }
(...skipping 21 matching lines...) Expand all
38 &status); 40 &status);
39 int matches_count = 0; 41 int matches_count = 0;
40 const UCharsetMatch** matches = ucsdet_detectAll(detector, 42 const UCharsetMatch** matches = ucsdet_detectAll(detector,
41 &matches_count, 43 &matches_count,
42 &status); 44 &status);
43 if (U_FAILURE(status)) { 45 if (U_FAILURE(status)) {
44 ucsdet_close(detector); 46 ucsdet_close(detector);
45 return false; 47 return false;
46 } 48 }
47 49
50 // ICU has some heuristics for encoding detection, such that the more likely
51 // encodings should be returned first. However, it doesn't always return
52 // all encodings that properly decode |text|, so we'll append more encodings
53 // later. To make that efficient, keep track of encodings sniffed in this
54 // first phase.
55 std::set<std::string> sniffed_encodings;
56
48 encodings->clear(); 57 encodings->clear();
49 for (int i = 0; i < matches_count; i++) { 58 for (int i = 0; i < matches_count; i++) {
50 UErrorCode get_name_status = U_ZERO_ERROR; 59 UErrorCode get_name_status = U_ZERO_ERROR;
51 const char* encoding_name = ucsdet_getName(matches[i], &get_name_status); 60 const char* encoding_name = ucsdet_getName(matches[i], &get_name_status);
52 61
53 // If we failed to get the encoding's name, ignore the error. 62 // If we failed to get the encoding's name, ignore the error.
54 if (U_FAILURE(get_name_status)) 63 if (U_FAILURE(get_name_status))
55 continue; 64 continue;
56 65
66 int32_t confidence = ucsdet_getConfidence(matches[i], &get_name_status);
67
68 // We also treat this error as non-fatal.
69 if (U_FAILURE(get_name_status))
70 continue;
71
72 // A confidence level >= 10 means that the encoding is expected to properly
73 // decode the text. Drop all encodings with lower confidence level.
74 if (confidence < 10)
75 continue;
76
57 encodings->push_back(encoding_name); 77 encodings->push_back(encoding_name);
78 sniffed_encodings.insert(encoding_name);
58 } 79 }
59 80
81 // Append all encodings not included earlier, in arbitrary order.
82 // TODO(jshin): This shouldn't be necessary, possible ICU bug.
83 // See also http://crbug.com/65917.
84 UEnumeration* detectable_encodings = ucsdet_getAllDetectableCharsets(detector,
85 &status);
86 int detectable_count = uenum_count(detectable_encodings, &status);
87 for (int i = 0; i < detectable_count; i++) {
88 int name_length;
89 const char* name_raw = uenum_next(detectable_encodings,
90 &name_length,
91 &status);
92 std::string name(name_raw, name_length);
93 if (sniffed_encodings.find(name) == sniffed_encodings.end())
94 encodings->push_back(name);
95 }
96 uenum_close(detectable_encodings);
97
60 ucsdet_close(detector); 98 ucsdet_close(detector);
61 return !encodings->empty(); 99 return !encodings->empty();
62 } 100 }
63 101
64 } // namespace base 102 } // namespace base
OLDNEW
« no previous file with comments | « base/i18n/icu_encoding_detection.h ('k') | net/data/ftp/dir-listing-ls-25 » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698