Giter VIP home page Giter VIP logo

chinese-util-cpp's Introduction

chinese-util-cpp

Actions Status Actions Status Actions Status Actions Status Actions Status codecov

介绍

C++ 实现的汉字转拼音、拼音分词、简繁互转、数字转换、金额数字转换。

支持与 PHP、Swoole 编译,支持在 PHP FFI 中运行。

编译

编译动态库

cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Release
sudo cmake --build build --target install --config Release -j

带 PHP 编译:

cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Release -DWITH_PHP=1
sudo cmake --build build --target install --config Release -j

带 Swoole 编译:

cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Release -DWITH_PHP=1 -DWITH_SWOOLE=1
sudo cmake --build build --target install --config Release -j

指定 PHP include 路径

如果 php-config 无法正常使用,可能会需要手动指定

cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Release -DWITH_PHP=1 -DWITH_SWOOLE=1 -DPHP_INCLUDE_DIR="/usr/include/php/20190902"
sudo cmake --build build --target install --config Release -j

指定 PHP include、lib 路径

Windows 系统需要用到

cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Release -DWITH_PHP=1 -DWITH_SWOOLE=1 -DPHP_INCLUDE_DIR="C:\Windows\php-dev\include" -DPHP_LIB_DIR="C:\Windows\php-dev\lib"
sudo cmake --build build --target install --config Release -j

测试用例

cmake -Htest -Bbuild/test
cmake --build build/test --config Debug -j
cmake --build build/test --target test

项目起源

2020 年 7 月份开始想研究 C++,那么研究点什么好呢。想着反哺一下 PHP 生态,那么就为我之前开发的 ChineseUtil 提升一下性能吧。

于是有了一个多月的折腾研究,起初是折腾 C++ 项目的配置。

然后是开发了 FFI C 函数,效果不是很理想。性能甚至都不如 PHP 算法,原因是 FFI 转换字符串数组,甚至是二维数组,效率实在是太低并且麻烦,有内存泄漏风险。

经过一番研究,尝试了一下目前的方案,也就是调用 zend_register_functions() 函数来动态创建 PHP 函数。

类似于 PHP 扩展的开发,但是是通过 FFI 来创建的。

那么问题来了,为啥不直接做成 PHP 扩展?

我的本意呢,是让 ChineseUtil 这个库可以开箱即用,不需要编译、启用扩展啥的,麻烦。

PHP dl() 函数也可以动态加载扩展,但是很多环境中是被禁用的。

那么 FFI 来动态创建 PHP 函数,就成了最佳方案(也许)。

chinese-util-cpp's People

Contributors

yurunsoft avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.