Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Properties FFI #1269

Merged
merged 14 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion components/properties/src/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use crate::*;
use icu_codepointtrie::TrieValue;
use icu_provider::prelude::*;

type CodePointMapResult<'data, T> =
/// TODO(#1239): Finalize this API.
pub type CodePointMapResult<'data, T> =
Result<DataPayload<'data, UnicodePropertyMapV1Marker<T>>, PropertiesError>;

fn get_cp_map<'data, D, T>(provider: &D, resc_key: ResourceKey) -> CodePointMapResult<'data, T>
Expand Down
3 changes: 2 additions & 1 deletion components/properties/src/sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use crate::provider::*;
use crate::*;
use icu_provider::prelude::*;

type UnisetResult<'data> = Result<DataPayload<'data, UnicodePropertyV1Marker>, PropertiesError>;
/// TODO(#1239): Finalize this API.
pub type UnisetResult<'data> = Result<DataPayload<'data, UnicodePropertyV1Marker>, PropertiesError>;

// helper fn
fn get_uniset<'data, D>(provider: &D, resc_key: ResourceKey) -> UnisetResult<'data>
Expand Down
1 change: 1 addition & 0 deletions ffi/diplomat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ icu_locale_canonicalizer = { path = "../../components/locale_canonicalizer" }
icu_locid = { path = "../../components/locid" }
icu_locid_macros = { path = "../../components/locid/macros" }
icu_plurals = { path = "../../components/plurals/" }
icu_properties = { path = "../../components/properties/" }
icu_provider = { path = "../../provider/core", features = ["provider_serde"] }
icu_provider_blob = { path = "../../provider/blob" }
tinystr = { version = "0.4.10", features = ["alloc"], default-features = false }
Expand Down
28 changes: 28 additions & 0 deletions ffi/diplomat/c/include/ICU4XUnicodeScriptMapProperty.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef ICU4XUnicodeScriptMapProperty_H
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(thought)

This file is auto-generated, right?

Would it be possible to have Diplomat add a "this file was autogenerated by ..." comment at the top of generated files, to make it easier for reviewers / readers of the code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That's a good idea. rust-diplomat/diplomat#100

#define ICU4XUnicodeScriptMapProperty_H
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "diplomat_runtime.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct ICU4XUnicodeScriptMapProperty ICU4XUnicodeScriptMapProperty;
#include "ICU4XDataProvider.h"
#include "ICU4XUnicodeScriptMapPropertyResult.h"
#include "ICU4XStaticDataProvider.h"

ICU4XUnicodeScriptMapPropertyResult ICU4XUnicodeScriptMapProperty_try_get(const ICU4XDataProvider* provider);

ICU4XUnicodeScriptMapPropertyResult ICU4XUnicodeScriptMapProperty_try_get_from_static(const ICU4XStaticDataProvider* provider);

uint32_t ICU4XUnicodeScriptMapProperty_get(const ICU4XUnicodeScriptMapProperty* self, char32_t cp);
void ICU4XUnicodeScriptMapProperty_destroy(ICU4XUnicodeScriptMapProperty* self);

#ifdef __cplusplus
}
#endif
#endif
24 changes: 24 additions & 0 deletions ffi/diplomat/c/include/ICU4XUnicodeScriptMapPropertyResult.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef ICU4XUnicodeScriptMapPropertyResult_H
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to have this in a separate file from ICU4XUnicodeScriptMapProperty.h, or is it just an implementation detail of Diplomat?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation detail of Diplomat.

#define ICU4XUnicodeScriptMapPropertyResult_H
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "diplomat_runtime.h"

#ifdef __cplusplus
extern "C" {
#endif
typedef struct ICU4XUnicodeScriptMapProperty ICU4XUnicodeScriptMapProperty;

typedef struct ICU4XUnicodeScriptMapPropertyResult {
ICU4XUnicodeScriptMapProperty* data;
bool success;
} ICU4XUnicodeScriptMapPropertyResult;

void ICU4XUnicodeScriptMapPropertyResult_destroy(ICU4XUnicodeScriptMapPropertyResult* self);

#ifdef __cplusplus
}
#endif
#endif
28 changes: 28 additions & 0 deletions ffi/diplomat/c/include/ICU4XUnicodeSetProperty.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef ICU4XUnicodeSetProperty_H
#define ICU4XUnicodeSetProperty_H
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "diplomat_runtime.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct ICU4XUnicodeSetProperty ICU4XUnicodeSetProperty;
#include "ICU4XDataProvider.h"
#include "ICU4XUnicodeSetPropertyResult.h"
#include "ICU4XStaticDataProvider.h"

ICU4XUnicodeSetPropertyResult ICU4XUnicodeSetProperty_try_get_ascii_hex_digit(const ICU4XDataProvider* provider);

ICU4XUnicodeSetPropertyResult ICU4XUnicodeSetProperty_try_get_ascii_hex_digit_from_static(const ICU4XStaticDataProvider* provider);

bool ICU4XUnicodeSetProperty_contains(const ICU4XUnicodeSetProperty* self, char32_t cp);
void ICU4XUnicodeSetProperty_destroy(ICU4XUnicodeSetProperty* self);

#ifdef __cplusplus
}
#endif
#endif
24 changes: 24 additions & 0 deletions ffi/diplomat/c/include/ICU4XUnicodeSetPropertyResult.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef ICU4XUnicodeSetPropertyResult_H
#define ICU4XUnicodeSetPropertyResult_H
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "diplomat_runtime.h"

#ifdef __cplusplus
extern "C" {
#endif
typedef struct ICU4XUnicodeSetProperty ICU4XUnicodeSetProperty;

typedef struct ICU4XUnicodeSetPropertyResult {
ICU4XUnicodeSetProperty* data;
bool success;
} ICU4XUnicodeSetPropertyResult;

void ICU4XUnicodeSetPropertyResult_destroy(ICU4XUnicodeSetPropertyResult* self);

#ifdef __cplusplus
}
#endif
#endif
2 changes: 2 additions & 0 deletions ffi/diplomat/cpp/docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Documentation
locale_canonicalizer_ffi
locale_ffi
pluralrules_ffi
properties_maps_ffi
properties_sets_ffi
provider_ffi

Indices and tables
Expand Down
28 changes: 28 additions & 0 deletions ffi/diplomat/cpp/docs/source/properties_maps_ffi.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
``properties_maps::ffi``
========================

.. cpp:class:: ICU4XCodePointMapData16

An ICU4X Unicode Set Property object, capable of querying whether a code point is contained in a set based on a Unicode property. For properties whose values fit into 16 bits. See `the Rust docs <https://unicode-org.github.io/icu4x-docs/doc/icu_properties/index.html>`__ for more information.

.. cpp:function:: static ICU4XCodePointMapData16Response try_get_script(const ICU4XDataProvider& provider)

Gets a map for Unicode property Script from a :cpp:class:`ICU4XDataProvider`. See `the Rust docs <https://unicode-org.github.io/icu4x-docs/doc/icu_properties/maps/fn.get_script.html>`__ for more information.

.. cpp:function:: static ICU4XCodePointMapData16Response try_get_script_from_static(const ICU4XStaticDataProvider& provider)

Gets a map for Unicode property Script from a :cpp:class:`ICU4XStaticDataProvider`. See `the Rust docs <https://unicode-org.github.io/icu4x-docs/doc/icu_properties/maps/fn.get_script.html>`__ for more information.

.. cpp:function:: uint16_t get(char32_t cp) const

Gets the value for a code point. See `the Rust docs <https://unicode-org.github.io/icu4x-docs/doc/icu_codepointtrie/codepointtrie/struct.CodePointTrie.html#method.get_u32>`__ for more information.

.. cpp:struct:: ICU4XCodePointMapData16Response

.. cpp:member:: std::optional<ICU4XCodePointMapData16> data

The :cpp:class:`ICU4XCodePointMapData16`, if creation was successful.

.. cpp:member:: bool success

Whether creating the :cpp:class:`ICU4XCodePointMapData16` was successful.
28 changes: 28 additions & 0 deletions ffi/diplomat/cpp/docs/source/properties_sets_ffi.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
``properties_sets::ffi``
========================

.. cpp:class:: ICU4XCodePointSetData

An ICU4X Unicode Set Property object, capable of querying whether a code point is contained in a set based on a Unicode property. See `the Rust docs <https://unicode-org.github.io/icu4x-docs/doc/icu_properties/index.html>`__ for more information.

.. cpp:function:: static ICU4XCodePointSetDataResult try_get_ascii_hex_digit(const ICU4XDataProvider& provider)

Gets a set for Unicode property ascii_hex_digit from a :cpp:class:`ICU4XDataProvider`. See `the Rust docs <https://unicode-org.github.io/icu4x-docs/doc/icu_properties/sets/fn.get_ascii_hex_digit.html>`__ for more information.

.. cpp:function:: static ICU4XCodePointSetDataResult try_get_ascii_hex_digit_from_static(const ICU4XStaticDataProvider& provider)

Gets a set for Unicode property ascii_hex_digit from a :cpp:class:`ICU4XStaticDataProvider`. See `the Rust docs <https://unicode-org.github.io/icu4x-docs/doc/icu_properties/sets/fn.get_ascii_hex_digit.html>`__ for more information.

.. cpp:function:: bool contains(char32_t cp) const

Checks whether the code point is in the set. See `the Rust docs <https://unicode-org.github.io/icu4x-docs/doc/icu_uniset/struct.UnicodeSet.html#method.contains>`__ for more information.

.. cpp:struct:: ICU4XCodePointSetDataResult

.. cpp:member:: std::optional<ICU4XCodePointSetData> data

The :cpp:class:`ICU4XCodePointSetData`, if creation was successful.

.. cpp:member:: bool success

Whether creating the :cpp:class:`ICU4XCodePointSetData` was successful.
2 changes: 2 additions & 0 deletions ffi/diplomat/cpp/examples/properties/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a.out
a.out.dSYM
27 changes: 27 additions & 0 deletions ffi/diplomat/cpp/examples/properties/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This file is part of ICU4X. For terms of use, please see the file
# called LICENSE at the top level of the ICU4X source tree
# (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

.DEFAULT_GOAL := test
.PHONY: build test

ALL_HEADERS := $(wildcard ../../include/*.hpp) $(wildcard ../../../c/include/*.h)
ALL_RUST := $(wildcard ../../../src/*.rs)

CXX?=g++

$(ALL_RUST):

$(ALL_HEADERS):


../../../../../target/debug/libicu_capi.a: $(ALL_RUST)
cargo build -p icu_capi

a.out: ../../../../../target/debug/libicu_capi.a $(ALL_HEADERS) test.cpp
$(CXX) -std=c++17 test.cpp ../../../../../target/debug/libicu_capi.a -ldl -lpthread -lm -g

build: a.out

test: build
./a.out
69 changes: 69 additions & 0 deletions ffi/diplomat/cpp/examples/properties/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

#include "../../include/ICU4XCodePointSetData.hpp"
#include "../../include/ICU4XCodePointMapData16.hpp"
#include "../../include/ICU4XCodePointMapData16Response.hpp"

#include <iostream>

const std::string_view path = "../../../../../provider/testdata/data/json/";

int test_set_property(ICU4XCodePointSetDataResult result, char32_t included, char32_t excluded) {
if (!result.success) {
std::cout << "Failed to create ICU4XCodePointSetData" << std::endl;
return 1;
}
bool contains1 = result.data.value().contains(included);
bool contains2 = result.data.value().contains(excluded);
std::cout << std::hex; // print hex for U+####
if (contains1 && !contains2) {
std::cout << "Set correctly contains U+" << included << " and not U+" << excluded << std::endl;
} else {
std::cout << "Set returns wrong result on U+" << included << " or U+" << excluded << std::endl;
return 1;
}
return 0;
}

int test_map_16_property(ICU4XCodePointMapData16Response result, char32_t sample, uint32_t expected) {
if (!result.success) {
std::cout << "Failed to create ICU4XCodePointMapData16" << std::endl;
return 1;
}
uint32_t actual = result.data.value().get(sample);
std::cout << std::hex; // print hex for U+####
if (actual == expected) {
std::cout << "Code point U+" << sample << " correctly mapped to 0x" << actual << std::endl;
} else {
std::cout << "Code point U+" << sample << " incorrectly mapped to 0x" << actual << std::endl;
return 1;
}
return 0;
}

int main() {
ICU4XDataProvider dp = ICU4XDataProvider::create_fs(path).provider.value();
int result;

result = test_set_property(
ICU4XCodePointSetData::try_get_ascii_hex_digit(dp),
u'3',
u'੩'
);
if (result != 0) {
return result;
}

result = test_map_16_property(
ICU4XCodePointMapData16::try_get_script(dp),
u'木',
17 // Script::Han
);
if (result != 0) {
return result;
}

return 0;
}
28 changes: 28 additions & 0 deletions ffi/diplomat/cpp/include/ICU4XCodePointMapData16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef ICU4XCodePointMapData16_H
#define ICU4XCodePointMapData16_H
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "diplomat_runtime.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct ICU4XCodePointMapData16 ICU4XCodePointMapData16;
#include "ICU4XDataProvider.h"
#include "ICU4XCodePointMapData16Response.h"
#include "ICU4XStaticDataProvider.h"

ICU4XCodePointMapData16Response ICU4XCodePointMapData16_try_get_script(const ICU4XDataProvider* provider);

ICU4XCodePointMapData16Response ICU4XCodePointMapData16_try_get_script_from_static(const ICU4XStaticDataProvider* provider);

uint16_t ICU4XCodePointMapData16_get(const ICU4XCodePointMapData16* self, char32_t cp);
void ICU4XCodePointMapData16_destroy(ICU4XCodePointMapData16* self);

#ifdef __cplusplus
}
#endif
#endif
Loading