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

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: trying to make things more clear 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
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 }
17 19
18 UErrorCode status = U_ZERO_ERROR; 20 UErrorCode status = U_ZERO_ERROR;
19 UCharsetDetector* detector = ucsdet_open(&status); 21 UCharsetDetector* detector = ucsdet_open(&status);
jungshik at Google 2011/04/01 18:28:41 You might consider using scoped_ptr_malloc. Well,
Paweł Hajdan Jr. 2011/04/01 18:45:42 Yeah, I also don't think it's worth it, especially
20 ucsdet_setText(detector, text.data(), static_cast<int32_t>(text.length()), 22 ucsdet_setText(detector, text.data(), static_cast<int32_t>(text.length()),
21 &status); 23 &status);
22 const UCharsetMatch* match = ucsdet_detect(detector, &status); 24 const UCharsetMatch* match = ucsdet_detect(detector, &status);
23 const char* detected_encoding = ucsdet_getName(match, &status); 25 const char* detected_encoding = ucsdet_getName(match, &status);
24 ucsdet_close(detector); 26 ucsdet_close(detector);
25 27
26 if (U_FAILURE(status)) 28 if (U_FAILURE(status))
27 return false; 29 return false;
28 30
29 *encoding = detected_encoding; 31 *encoding = detected_encoding;
30 return true; 32 return true;
31 } 33 }
32 34
33 bool DetectAllEncodings(const std::string& text, 35 bool DetectAllEncodings(const std::string& text,
34 std::vector<std::string>* encodings) { 36 std::vector<std::string>* encodings) {
35 UErrorCode status = U_ZERO_ERROR; 37 UErrorCode status = U_ZERO_ERROR;
36 UCharsetDetector* detector = ucsdet_open(&status); 38 UCharsetDetector* detector = ucsdet_open(&status);
37 ucsdet_setText(detector, text.data(), static_cast<int32_t>(text.length()), 39 ucsdet_setText(detector, text.data(), static_cast<int32_t>(text.length()),
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.
jungshik at Google 2011/04/01 18:28:41 hmm... if ucsdet_detectAll does not return all the
Paweł Hajdan Jr. 2011/04/01 18:45:42 See the raw listing attached to the bug. That was
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
57 encodings->push_back(encoding_name); 66 encodings->push_back(encoding_name);
67 sniffed_encodings.insert(encoding_name);
58 } 68 }
59 69
70 // Append all encodings not included earlier, in arbitrary order.
jungshik at Google 2011/04/01 18:28:41 This behavior should be documented in the header f
Paweł Hajdan Jr. 2011/04/01 18:45:42 Well, even before this change that was true, i.e.
71 UEnumeration* detectable_encodings = ucsdet_getAllDetectableCharsets(detector,
72 &status);
73 int detectable_count = uenum_count(detectable_encodings, &status);
74 for (int i = 0; i < detectable_count; i++) {
75 int name_length;
76 const char* name_raw = uenum_next(detectable_encodings,
77 &name_length,
78 &status);
79 std::string name(name_raw, name_length);
80 if (sniffed_encodings.find(name) == sniffed_encodings.end())
81 encodings->push_back(name);
82 }
83 uenum_close(detectable_encodings);
84
60 ucsdet_close(detector); 85 ucsdet_close(detector);
61 return !encodings->empty(); 86 return !encodings->empty();
62 } 87 }
63 88
64 } // namespace base 89 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | net/data/ftp/dir-listing-ls-25 » ('j') | net/ftp/ftp_directory_listing_parser_ls.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698