Skip to content

Commit

Permalink
Use ICU's C API only
Browse files Browse the repository at this point in the history
Replace the C++ ICU function icu::toUCharPtr with the locally
implemented to_UChar_ptr function.
  • Loading branch information
rmisev committed Jul 4, 2024
1 parent 0877b18 commit 4bc9711
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ project licensed as follows:

-------------------------------------------------------------------------------

Files url_utf.cpp, url_utf.h contains portions of modified code from the ICU
project licensed as follows:
Files config.h, url_utf.cpp, url_utf.h contains portions of modified code from
the ICU project licensed as follows:

UNICODE LICENSE V3

Expand Down
14 changes: 13 additions & 1 deletion include/upa/config.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016-2023 Rimas Misevičius
// Copyright 2016-2024 Rimas Misevičius
// Distributed under the BSD-style license that can be
// found in the LICENSE file.
//
Expand Down Expand Up @@ -39,4 +39,16 @@
# define UPA_CONSTEXPR_14 inline
#endif

// Barrier for pointer anti-aliasing optimizations even across function boundaries.
// This is a slightly modified U_ALIASING_BARRIER macro from the char16ptr.h file
// of the ICU 75.1 library.
// Discussion: https://github.com/sg16-unicode/sg16/issues/67
#ifndef UPA_ALIASING_BARRIER
# if defined(__clang__) || defined(__GNUC__)
# define UPA_ALIASING_BARRIER(ptr) asm volatile("" : : "rm"(ptr) : "memory"); // NOLINT(*-macro-*)
# else
# define UPA_ALIASING_BARRIER(ptr)
# endif
#endif

#endif // UPA_CONFIG_H
32 changes: 19 additions & 13 deletions src/url_idna.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016-2023 Rimas Misevičius
// Copyright 2016-2024 Rimas Misevičius
// Distributed under the BSD-style license that can be
// found in the LICENSE file.
//
Expand All @@ -7,15 +7,15 @@
// Copyright 2013 The Chromium Authors. All rights reserved.
//

#include "upa/config.h"
#include "upa/url_idna.h"
#include "upa/util.h"
// ICU
// ICU: only C API is used (U_SHOW_CPLUSPLUS_API 0)
// https://unicode-org.github.io/icu/userguide/icu4c/build.html#icu-as-a-system-level-library
#define U_SHOW_CPLUSPLUS_API 0 // NOLINT(*-macro-*)
#include "unicode/uchar.h" // u_getUnicodeVersion
#include "unicode/uclean.h"
#include "unicode/uidna.h"
#if (U_ICU_VERSION_MAJOR_NUM) >= 59
# include "unicode/char16ptr.h"
#endif
#if (U_ICU_VERSION_MAJOR_NUM) < 68
# include <algorithm>
#endif
Expand Down Expand Up @@ -75,15 +75,21 @@ unsigned idna_unicode_version() {

// Conversion to ICU UChar

namespace {

static_assert(sizeof(UChar) == sizeof(char16_t), "UChar must be the same size as char16_t");

#if (U_ICU_VERSION_MAJOR_NUM) < 59
// toUCharPtr functions are defined in ICU 59
namespace icu {
inline const UChar* toUCharPtr(const char16_t* p) { return reinterpret_cast<const UChar*>(p); }
inline UChar* toUCharPtr(char16_t* p) { return reinterpret_cast<UChar*>(p); }
inline const UChar* to_UChar_ptr(const char16_t* p) noexcept {
UPA_ALIASING_BARRIER(p)
return reinterpret_cast<const UChar*>(p);
}
#endif

inline UChar* to_UChar_ptr(char16_t* p) noexcept {
UPA_ALIASING_BARRIER(p)
return reinterpret_cast<UChar*>(p);
}

} // namespace

// Implements the domain to ASCII algorithm
// https://url.spec.whatwg.org/#concept-domain-to-ascii
Expand Down Expand Up @@ -117,8 +123,8 @@ validation_errc domain_to_ascii(const char16_t* src, std::size_t src_len, simple
UErrorCode err = U_ZERO_ERROR;
UIDNAInfo info = UIDNA_INFO_INITIALIZER;
const int32_t output_length = uidna_nameToASCII(uidna,
icu::toUCharPtr(src), static_cast<int32_t>(src_len),
icu::toUCharPtr(output.data()), static_cast<int32_t>(output.capacity()),
to_UChar_ptr(src), static_cast<int32_t>(src_len),
to_UChar_ptr(output.data()), static_cast<int32_t>(output.capacity()),
&info, &err);
if (U_SUCCESS(err) && (info.errors & UIDNA_ERR_MASK) == 0) {
output.resize(output_length);
Expand Down

0 comments on commit 4bc9711

Please sign in to comment.