1 // Copyright 2024 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef URL_URL_CANON_ICU_TEST_HELPERS_H_ 6 #define URL_URL_CANON_ICU_TEST_HELPERS_H_ 7 8 #include "base/logging.h" 9 #include "third_party/icu/source/common/unicode/ucnv.h" 10 #include "url/url_canon.h" 11 12 namespace url::test { 13 14 // Wrapper around a UConverter object that managers creation and destruction. 15 class UConvScoper { 16 public: UConvScoper(const char * charset_name)17 explicit UConvScoper(const char* charset_name) { 18 UErrorCode err = U_ZERO_ERROR; 19 converter_ = ucnv_open(charset_name, &err); 20 if (!converter_) { 21 LOG(ERROR) << "Failed to open charset " << charset_name << ": " 22 << u_errorName(err); 23 } 24 } 25 ~UConvScoper()26 ~UConvScoper() { 27 if (converter_) { 28 ucnv_close(converter_.ExtractAsDangling()); 29 } 30 } 31 32 // Returns the converter object, may be NULL. converter()33 UConverter* converter() const { return converter_; } 34 35 private: 36 raw_ptr<UConverter> converter_; 37 }; 38 39 } // namespace url::test 40 41 #endif // URL_URL_CANON_ICU_TEST_HELPERS_H_ 42