Skip to content

Commit

Permalink
Initial Properties FFI (#1269)
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc authored Nov 15, 2021
1 parent cfc5fda commit 242fa55
Show file tree
Hide file tree
Showing 30 changed files with 976 additions and 2 deletions.
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.

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

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

fn get_cp_map<D, T>(provider: &D, resc_key: ResourceKey) -> CodePointMapResult<T>
where
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 = Result<DataPayload<UnicodePropertyV1Marker>, PropertiesError>;
/// TODO(#1239): Finalize this API.
pub type UnisetResult = Result<DataPayload<UnicodePropertyV1Marker>, PropertiesError>;

// helper fn
fn get_uniset<D>(provider: &D, resc_key: ResourceKey) -> UnisetResult
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/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
24 changes: 24 additions & 0 deletions ffi/diplomat/c/include/ICU4XCodePointMapData16Response.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef ICU4XCodePointMapData16Response_H
#define ICU4XCodePointMapData16Response_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;

typedef struct ICU4XCodePointMapData16Response {
ICU4XCodePointMapData16* data;
bool success;
} ICU4XCodePointMapData16Response;

void ICU4XCodePointMapData16Response_destroy(ICU4XCodePointMapData16Response* self);

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

#ifdef __cplusplus
extern "C" {
#endif

typedef struct ICU4XCodePointSetData ICU4XCodePointSetData;
#include "ICU4XDataProvider.h"
#include "ICU4XCodePointSetDataResult.h"
#include "ICU4XStaticDataProvider.h"

ICU4XCodePointSetDataResult ICU4XCodePointSetData_try_get_ascii_hex_digit(const ICU4XDataProvider* provider);

ICU4XCodePointSetDataResult ICU4XCodePointSetData_try_get_ascii_hex_digit_from_static(const ICU4XStaticDataProvider* provider);

bool ICU4XCodePointSetData_contains(const ICU4XCodePointSetData* self, char32_t cp);
void ICU4XCodePointSetData_destroy(ICU4XCodePointSetData* self);

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

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

typedef struct ICU4XCodePointSetDataResult {
ICU4XCodePointSetData* data;
bool success;
} ICU4XCodePointSetDataResult;

void ICU4XCodePointSetDataResult_destroy(ICU4XCodePointSetDataResult* 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

0 comments on commit 242fa55

Please sign in to comment.