Skip to content

Commit

Permalink
feat(c): add C binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaoses-Ib committed Dec 15, 2023
1 parent b2bdfcd commit 9d2f3f0
Show file tree
Hide file tree
Showing 15 changed files with 484 additions and 4 deletions.
127 changes: 126 additions & 1 deletion Cargo.lock

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

18 changes: 17 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
[workspace]
members = ["bindings/c"]

[workspace.package]
version = "0.2.0"
authors = ["Chaoses-Ib"]
homepage = "https://github.com/Chaoses-Ib/IbPinyinLib"
repository = "https://github.com/Chaoses-Ib/IbPinyinLib"
license = "MIT"
keywords = ["pinyin", "cjk"]

[package]
name = "ib-pinyin"
version = "0.1.0"
edition = "2021"
version.workspace = true
authors.workspace = true
homepage.workspace = true
repository.workspace = true
license.workspace = true
keywords.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
# IbPinyinLib
## 语言
- [C++ 实现](C++/README.md)(停止维护)
### Rust
```rust
use ib_pinyin::{matcher::PinyinMatcher, pinyin::PinyinNotation};

let matcher = PinyinMatcher::builder("pysousuoeve")
.pinyin_notations(PinyinNotation::Ascii | PinyinNotation::AsciiFirstLetter)
.build();
assert!(matcher.is_match("拼音搜索Everything"));
```

### C
```c
#include <ib_pinyin/ib_pinyin.h>
#include <ib_pinyin/notation.h>

bool is_match = ib_pinyin_is_match_u8c(u8"pysousuoeve", u8"拼音搜索Everything", PINYIN_NOTATION_ASCII_FIRST_LETTER | PINYIN_NOTATION_ASCII);
```

### C++
[原实现](C++/README.md)(停止维护)

## 相关项目
- [IbEverythingExt: Everything 拼音搜索、快速选择扩展](https://github.com/Chaoses-Ib/IbEverythingExt)
Expand Down
19 changes: 19 additions & 0 deletions bindings/c/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "ib-pinyin-c"
edition = "2021"
version.workspace = true
authors.workspace = true
homepage.workspace = true
repository.workspace = true
license.workspace = true
keywords.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["staticlib", "cdylib"]

[dependencies]
diplomat = "0.7.0"
diplomat-runtime = "0.7.0"
ib-pinyin = { path = "../..", features = ["minimal"] }
6 changes: 6 additions & 0 deletions bindings/c/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# IbPinyinLib.C
```sh
diplomat-tool c bindings/c/include/ib_pinyin -e bindings/c/src/lib.rs
```

Manually update: `notation.h`
3 changes: 3 additions & 0 deletions bindings/c/examples/cmake/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vs/

out/
18 changes: 18 additions & 0 deletions bindings/c/examples/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# CMakeList.txt : CMake project for cmake, include source and define
# project specific logic here.
#
cmake_minimum_required(VERSION 3.8)

project("cmake")

# Add source to this project's executable.
add_executable(cmake "main.c")

target_include_directories(cmake PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../include)

# target_link_libraries(cmake PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../../../target/debug/ib_pinyin_c.lib)
target_link_libraries(cmake PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../../../target/debug/ib_pinyin_c.dll.lib)
add_custom_command(TARGET cmake POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${CMAKE_CURRENT_SOURCE_DIR}/../../../../target/debug/ib_pinyin_c.dll"
$<TARGET_FILE_DIR:cmake>)
61 changes: 61 additions & 0 deletions bindings/c/examples/cmake/CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"version": 3,
"configurePresets": [
{
"name": "windows-base",
"hidden": true,
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Windows"
}
},
{
"name": "x64-debug",
"displayName": "x64 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x64-release",
"displayName": "x64 Release",
"inherits": "x64-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "x86-debug",
"displayName": "x86 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x86",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x86-release",
"displayName": "x86 Release",
"inherits": "x86-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
]
}
18 changes: 18 additions & 0 deletions bindings/c/examples/cmake/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdio.h>
#include <string.h>
#include <ib_pinyin/ib_pinyin.h>
#include <ib_pinyin/notation.h>

int main()
{
const char *pattern = u8"pysousuoeve";
const char *haystack = u8"拼音搜索Everything";
// 0x3
const PinyinNotation notations = PINYIN_NOTATION_ASCII_FIRST_LETTER | PINYIN_NOTATION_ASCII;

printf("%d\n", ib_pinyin_is_match_u8(pattern, strlen(pattern), haystack, strlen(haystack), notations));

printf("%d\n", ib_pinyin_is_match_u8c(pattern, haystack, notations));

return 0;
}
Loading

0 comments on commit 9d2f3f0

Please sign in to comment.