| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 // File utilities that use the ICU library go in this file. Functions using ICU | 5 // File utilities that use the ICU library go in this file. Functions using ICU |
| 6 // are separated from the other functions to prevent ICU being pulled in by the | 6 // are separated from the other functions to prevent ICU being pulled in by the |
| 7 // linker if there is a false dependency. | 7 // linker if there is a false dependency. |
| 8 // | 8 // |
| 9 // (The VS2005 linker finds such a false dependency and adds ~300K of ICU to | 9 // (The VS2005 linker finds such a false dependency and adds ~300K of ICU to |
| 10 // chrome.exe if this code lives in file_util.cc, even though none of this code | 10 // chrome.exe if this code lives in file_util.cc, even though none of this code |
| 11 // is called.) | 11 // is called.) |
| 12 | 12 |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 | 14 |
| 15 #include "base/singleton.h" | 15 #include "base/singleton.h" |
| 16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "unicode/uniset.h" | 17 #include "unicode/uniset.h" |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 class IllegalCharacters { | 20 class IllegalCharacters { |
| 21 public: | 21 public: |
| 22 bool contains(UChar32 ucs4) { | 22 bool contains(UChar32 ucs4) { |
| 23 return !!set->contains(ucs4); | 23 return !!set->contains(ucs4); |
| 24 } | 24 } |
| 25 | 25 |
| 26 bool containsNone(const string16 &s) { | 26 bool containsNone(const string16 &s) { |
| 27 return !!set->containsNone(UnicodeString(s.c_str(), s.size())); | 27 return !!set->containsNone(icu::UnicodeString(s.c_str(), s.size())); |
| 28 } | 28 } |
| 29 | 29 |
| 30 private: | 30 private: |
| 31 friend class Singleton<IllegalCharacters>; | 31 friend class Singleton<IllegalCharacters>; |
| 32 friend struct DefaultSingletonTraits<IllegalCharacters>; | 32 friend struct DefaultSingletonTraits<IllegalCharacters>; |
| 33 | 33 |
| 34 IllegalCharacters(); | 34 IllegalCharacters(); |
| 35 ~IllegalCharacters() { } | 35 ~IllegalCharacters() { } |
| 36 | 36 |
| 37 scoped_ptr<UnicodeSet> set; | 37 scoped_ptr<icu::UnicodeSet> set; |
| 38 | 38 |
| 39 DISALLOW_COPY_AND_ASSIGN(IllegalCharacters); | 39 DISALLOW_COPY_AND_ASSIGN(IllegalCharacters); |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 IllegalCharacters::IllegalCharacters() { | 42 IllegalCharacters::IllegalCharacters() { |
| 43 UErrorCode status = U_ZERO_ERROR; | 43 UErrorCode status = U_ZERO_ERROR; |
| 44 // Control characters, formatting characters, non-characters, and | 44 // Control characters, formatting characters, non-characters, and |
| 45 // some printable ASCII characters regarded as dangerous ('"*/:<>?\\'). | 45 // some printable ASCII characters regarded as dangerous ('"*/:<>?\\'). |
| 46 // See http://blogs.msdn.com/michkap/archive/2006/11/03/941420.aspx | 46 // See http://blogs.msdn.com/michkap/archive/2006/11/03/941420.aspx |
| 47 // and http://msdn2.microsoft.com/en-us/library/Aa365247.aspx | 47 // and http://msdn2.microsoft.com/en-us/library/Aa365247.aspx |
| 48 // TODO(jungshik): Revisit the set. ZWJ and ZWNJ are excluded because they | 48 // TODO(jungshik): Revisit the set. ZWJ and ZWNJ are excluded because they |
| 49 // are legitimate in Arabic and some S/SE Asian scripts. However, when used | 49 // are legitimate in Arabic and some S/SE Asian scripts. However, when used |
| 50 // elsewhere, they can be confusing/problematic. | 50 // elsewhere, they can be confusing/problematic. |
| 51 // Also, consider wrapping the set with our Singleton class to create and | 51 // Also, consider wrapping the set with our Singleton class to create and |
| 52 // freeze it only once. Note that there's a trade-off between memory and | 52 // freeze it only once. Note that there's a trade-off between memory and |
| 53 // speed. | 53 // speed. |
| 54 #if defined(WCHAR_T_IS_UTF16) | 54 #if defined(WCHAR_T_IS_UTF16) |
| 55 set.reset(new UnicodeSet(UnicodeString( | 55 set.reset(new icu::UnicodeSet(icu::UnicodeString( |
| 56 L"[[\"*/:<>?\\\\|][:Cc:][:Cf:] - [\u200c\u200d]]"), status)); | 56 L"[[\"*/:<>?\\\\|][:Cc:][:Cf:] - [\u200c\u200d]]"), status)); |
| 57 #else | 57 #else |
| 58 set.reset(new UnicodeSet(UNICODE_STRING_SIMPLE( | 58 set.reset(new icu::UnicodeSet(UNICODE_STRING_SIMPLE( |
| 59 "[[\"*/:<>?\\\\|][:Cc:][:Cf:] - [\\u200c\\u200d]]").unescape(), | 59 "[[\"*/:<>?\\\\|][:Cc:][:Cf:] - [\\u200c\\u200d]]").unescape(), |
| 60 status)); | 60 status)); |
| 61 #endif | 61 #endif |
| 62 DCHECK(U_SUCCESS(status)); | 62 DCHECK(U_SUCCESS(status)); |
| 63 // Add non-characters. If this becomes a performance bottleneck by | 63 // Add non-characters. If this becomes a performance bottleneck by |
| 64 // any chance, do not add these to |set| and change IsFilenameLegal() | 64 // any chance, do not add these to |set| and change IsFilenameLegal() |
| 65 // to check |ucs4 & 0xFFFEu == 0xFFFEu|, in addiition to calling | 65 // to check |ucs4 & 0xFFFEu == 0xFFFEu|, in addiition to calling |
| 66 // containsNone(). | 66 // containsNone(). |
| 67 set->add(0xFDD0, 0xFDEF); | 67 set->add(0xFDD0, 0xFDEF); |
| 68 for (int i = 0; i <= 0x10; ++i) { | 68 for (int i = 0; i <= 0x10; ++i) { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 (*file_name)[i] = replace_char; | 120 (*file_name)[i] = replace_char; |
| 121 } | 121 } |
| 122 ++i; | 122 ++i; |
| 123 } | 123 } |
| 124 #else | 124 #else |
| 125 #error wchar_t* should be either UTF-16 or UTF-32 | 125 #error wchar_t* should be either UTF-16 or UTF-32 |
| 126 #endif | 126 #endif |
| 127 } | 127 } |
| 128 | 128 |
| 129 } // namespace | 129 } // namespace |
| OLD | NEW |