-
Notifications
You must be signed in to change notification settings - Fork 183
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
Add basic C API for PluralRules #552
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3b4a3e5
Add rust-side C API crate, with code for plural rules
Manishearth 25b6f0f
Add headers
Manishearth f07d916
Add ctest
Manishearth 8af3197
Make some things const
Manishearth 74e4b9e
Rename ICU4XErasedDataProvider -> ICU4XDataProvider
Manishearth b7402ab
Replace into with from
Manishearth f5de1d7
Add comments and docs
Manishearth bc8782a
rename icu4x_erased_data_provider_destroy -> icu4x_data_provider_destroy
Manishearth 7673b82
fmt
Manishearth f5dc055
Add cargo-make target for ctest
Manishearth 26c68f2
Move pluralrules ctest to examples
Manishearth 48e12ce
goodby #pragma once :-'(
Manishearth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# 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 ). | ||
|
||
[package] | ||
name = "icu_capi" | ||
version = "0.1.0" | ||
authors = ["The ICU4X Developers"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
crate-type = ["staticlib", "rlib"] | ||
|
||
[dependencies] | ||
icu_locid = { path = "../locid" } | ||
icu_plurals = { path = "../plurals/" } | ||
icu_provider = { path = "../provider" } | ||
icu_provider_fs = { path = "../provider_fs/" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# 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/*.h) | ||
ALL_RUST := $(wildcard ../../src/*.rs) | ||
|
||
$(ALL_RUST): | ||
|
||
$(ALL_HEADERS): | ||
|
||
|
||
../../../../target/debug/libicu_capi.a: $(ALL_RUST) | ||
cargo build | ||
|
||
a.out: ../../../../target/debug/libicu_capi.a $(ALL_HEADERS) test.c | ||
gcc test.c ../../../../target/debug/libicu_capi.a -ldl -lpthread -lm | ||
|
||
build: a.out | ||
|
||
test: build | ||
./a.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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/pluralrules.h" | ||
#include <string.h> | ||
#include <stdio.h> | ||
|
||
const char* path = "../../../../resources/testdata/data/json/"; | ||
int main() { | ||
ICU4XLocale* locale = icu4x_locale_create("ar", 2); | ||
ICU4XCreateDataProviderResult result = icu4x_fs_data_provider_create(path, strlen(path)); | ||
if (!result.success) { | ||
printf("Failed to create FsDataProvider\n"); | ||
return 1; | ||
} | ||
ICU4XDataProvider provider = result.provider; | ||
ICU4XCreatePluralRulesResult plural_result = icu4x_plural_rules_create(locale, &provider, ICU4XPluralRuleType_Cardinal); | ||
if (!plural_result.success) { | ||
printf("Failed to create PluralRules\n"); | ||
return 1; | ||
} | ||
ICU4XPluralRules* rules = plural_result.rules; | ||
|
||
ICU4XPluralOperands op; | ||
op.i = 3; | ||
|
||
ICU4XPluralCategory cat = icu4x_plural_rules_select(rules, &op); | ||
|
||
printf("Plural Category %d (should be %d)\n", (int)cat, (int)ICU4XPluralCategory_Few); | ||
|
||
icu4x_plural_rules_destroy(rules); | ||
icu4x_data_provider_destroy(provider); | ||
icu4x_locale_destroy(locale); | ||
|
||
if (cat != ICU4XPluralCategory_Few) { | ||
return 1; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// 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 ). | ||
|
||
#ifndef ICU4X_LOCALE_H | ||
#define ICU4X_LOCALE_H | ||
|
||
#include <stdint.h> | ||
#include <stddef.h> | ||
#include <stdbool.h> | ||
|
||
// opaque | ||
typedef struct ICU4XLocale ICU4XLocale; | ||
|
||
ICU4XLocale* icu4x_locale_create(const char* value, size_t len); | ||
void icu4x_locale_destroy(ICU4XLocale*); | ||
|
||
#endif // ICU4X_LOCALE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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 ). | ||
|
||
#ifndef ICU4X_PLURALRULES_H | ||
#define ICU4X_PLURALRULES_H | ||
|
||
#include <stdint.h> | ||
#include <stddef.h> | ||
#include <stdbool.h> | ||
#include "provider.h" | ||
#include "locale.h" | ||
|
||
// opaque | ||
typedef struct ICU4XPluralRules ICU4XPluralRules; | ||
|
||
typedef struct { | ||
ICU4XPluralRules* rules; | ||
bool success; | ||
} ICU4XCreatePluralRulesResult; | ||
|
||
typedef enum { | ||
ICU4XPluralRuleType_Cardinal, | ||
ICU4XPluralRuleType_Ordinal | ||
} ICU4XPluralRuleType; | ||
|
||
typedef enum { | ||
ICU4XPluralCategory_Zero, | ||
ICU4XPluralCategory_One, | ||
ICU4XPluralCategory_Two, | ||
ICU4XPluralCategory_Few, | ||
ICU4XPluralCategory_Many, | ||
ICU4XPluralCategory_Other, | ||
} ICU4XPluralCategory; | ||
|
||
typedef struct { | ||
uint64_t i; | ||
size_t v; | ||
size_t w; | ||
uint64_t f; | ||
uint64_t t; | ||
size_t c; | ||
} ICU4XPluralOperands; | ||
|
||
|
||
ICU4XCreatePluralRulesResult icu4x_plural_rules_create(const ICU4XLocale* locale, const ICU4XDataProvider* provider, ICU4XPluralRuleType ty); | ||
ICU4XPluralCategory icu4x_plural_rules_select(const ICU4XPluralRules* rules, const ICU4XPluralOperands* op); | ||
void icu4x_plural_rules_destroy(ICU4XPluralRules* rules); | ||
|
||
#endif // ICU4X_PLURALRULES_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// 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 ). | ||
|
||
#ifndef ICU4X_PROVIDER_H | ||
#define ICU4X_PROVIDER_H | ||
|
||
#include <stdint.h> | ||
#include <stddef.h> | ||
#include <stdbool.h> | ||
|
||
typedef struct { | ||
uintptr_t _field1; | ||
uintptr_t _field2; | ||
} ICU4XDataProvider; | ||
|
||
typedef struct { | ||
ICU4XDataProvider provider; | ||
bool success; | ||
} ICU4XCreateDataProviderResult; | ||
|
||
void icu4x_data_provider_destroy(ICU4XDataProvider d); | ||
|
||
ICU4XCreateDataProviderResult icu4x_fs_data_provider_create(const char* path, size_t len); | ||
|
||
#endif // ICU4X_PROVIDER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// 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 ). | ||
|
||
pub mod locale; | ||
pub mod pluralrules; | ||
pub mod provider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// 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 ). | ||
|
||
use std::slice; | ||
|
||
use icu_locid::Locale; | ||
|
||
/// Opaque type for use behind a pointer, is [`Locale`] | ||
/// | ||
/// Can be obtained via [`icu4x_locale_create()`] and destroyed via [`icu4x_locale_destroy()`] | ||
pub type ICU4XLocale = Locale; | ||
|
||
#[no_mangle] | ||
/// FFI version of [`Locale::from_bytes()`], see its docs for more details | ||
/// | ||
/// # Safety | ||
/// `value` and `len` should point to a valid ASCII string of length `len`. | ||
/// | ||
/// It does not need to be be null terminated, and `len` should not include a null | ||
/// terminator (this will just cause the function to panic, and is not a safety requirement). | ||
pub unsafe extern "C" fn icu4x_locale_create(value: *const u8, len: usize) -> *mut ICU4XLocale { | ||
let bytes = slice::from_raw_parts(value, len); | ||
// todo: return errors | ||
let loc = ICU4XLocale::from_bytes(bytes).unwrap(); | ||
Box::into_raw(Box::new(loc)) | ||
} | ||
|
||
#[no_mangle] | ||
/// Destructor for [`ICU4XLocale`]. | ||
/// | ||
/// # Safety | ||
/// | ||
/// `loc` must be a pointer to a locale allocated by `icu4x_locale_destroy`. | ||
pub unsafe extern "C" fn icu4x_locale_destroy(loc: *mut ICU4XLocale) { | ||
let _ = Box::from_raw(loc); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So in this case, a nullptr for rules could indicate failure, so success is redundant. If results are always going to be ok or error, we could just use the pointer to indicate this. If not, then maybe we should have an enum of possible results, which could just be Ok/Error for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking of adding a proper error enum to this later, the
success
is just a placeholder.