nimayneb/yawl
Composer 安装命令:
composer require nimayneb/yawl
包简介
YAWL - Yet another wildcard library - Match pattern for strings with asterisk and query token
关键字:
README 文档
README
This is a library with classes for any wildcard implementation that finds pattern with * (asterisk) and ? (query) token.
Problem
Without regular expression extension (named ext-pcre - before PHP 5.3) you have no wildcard support within PHP.
See https://www.php.net/manual/en/pcre.installation.php.
The known wildcard behavior (see Wildcard character (@Wikipedia)) is limited for a good implementation.
A small remedy?
We need a kind of "compiled" and "cached" pattern. The implementation of regular expression like preg_match has a
huge performance. We lost these advantage as of PHP 7.0, because of just-in-time compiled pcre pattern!
Table of content
- Benchmark
- Wildcard variants
- Possible valid pattern
- Invalid pattern
- Escaping
- Repeating phrases
- Caching
- Wish list
- Appendix
API:
- Matcher (for use of single calls)
- Performer (for use of multiple calls)
- Converter (for use of regular expression)
Benchmark
When we benchmark several methods to match a fitting phrase (1000 random strings), we have a good results without "regular expressions":
Single Byte:
| Benchmark | Time | Reference | Difference |
|---|---|---|---|
WildcardPerformer |
0.00353789 | 100 % | - |
preg_match |
0.01223898 | 71 % | 71 % |
WildcardMatcher |
0.01635289 | 25 % | 78 % |
Multi Byte (like Unicode):
| Benchmark | Time | Reference | Difference |
|---|---|---|---|
WildcardPerformer |
0.00660086 | 100 % | - |
mb_ereg_match |
0.01290488 | 48 % | 48 % |
WildcardMatcher |
0.03252506 | 60 % | 79 % |
Internal PHP functions comparison
| Function | WildcardMatcher | WildcardPerformer |
|---|---|---|
| strlen | 9 | 5 (-4) |
| substr | 5 | 4 (-1) |
| strpos | 5 | 1 (-4) |
| chr | 2 | 2 (0) |
| Conditions | 57 | 13 (-44) |
Wildcard variants
| != character | == 1 character | >= 2 characters | Token |
|---|---|---|---|
| 0 | 0 | 0 | (invalid) |
| 0 | 0 | 1 | ??... / ??...* |
| 0 | 1 | 0 | ? |
| 0 | 1 | 1 | ** |
| 1 | 0 | 0 | (empty string) |
| 1 | 0 | 1 | ??** (draft) |
| 1 | 1 | 0 | ?* |
| 1 | 1 | 1 | * |
Possible valid pattern
?? (2 characters)
???* (2-3 characters)
Invalid pattern
***
?**
?*?
*?
Escaping
\\
\?
\*
Please note of this escaping scenarios:
| Subject | Pattern | Explanation |
|---|---|---|
search\\* |
*\\* |
matches search\ with wildcard, then matches escaped * |
search\\* |
*\\\\* |
matches search with wildcard, then matches escaped \ , then matches * with wildcard |
search\\* |
*\\\\\\* |
matches search with wildcard, then matches escaped \ , then matches escaped * |
search\\* |
*\\\\\\\\* |
matches search with wildcard, then matches escaped \ , then not matches escaped \ - ignore rest of * |
Further explanation:
| programmatic | internal | after escaping |
|---|---|---|
\\ |
\ |
\ |
\\\\ |
\\ |
\ |
Repeating phrases
The asterisk (*) has a problem finding the right position. If several identical phrases can be found in a string,
the search must take place in all positions to find the pattern.
Search: *is?ue (where is "*is")
Subject: this is an asterisk issue
Founds: ^ ^ ^ ^
The simplest solution is to break down the pattern recursively, but it should be not recursively.
Caching
The regular expression extension has a caching and compiling strategy to improve performance. The second time the same pattern is called, a tremendous increase in performance is achieved.
To help us to improve also performance, we use a simple key-value caching.
protected array $cachedResults = [];
public function match(string $subject): bool
{
return $this->cachedResults[$subject] ?? $this->cachedResults[$subject] = $this->computePhrases($subject, 0);
}
This is not a preferred solution.
Wish list
- Remove recursive call of
WildcardPerformer::computePhrases(see Wikipedia - Matching wildcards) - Remove of
StringFunctionMapper(too slow) - Combine Matcher and Performer (two advantages in one)
- Caching interface
- New pattern
??**(0 or 2 characters) or?????**(0 or 5 characters) - Glob support
- Example:
[abcdef0123456789],[0-9a-f],[!a-z] - Use in YAWL:
[0-9a-f]?(one times)[0-9a-f]?*(zero or one times)[0-9a-f]*(zero or N times)[0-9a-f]**(one or N times)[0-9a-f]x(one times then followsx)
- Example:
Appendix
Common Algorithms:
Non-recursive Algorithms:
Recursive Algorithms:
- Compares a filename or pathname to a pattern by "Apple"
- Stackoverflow - Recursive solutions for glob pattern matching by "Siler"
PDepend
- NOP - Number Of Packages
- NOC - Number Of Classes
- NOM - Number Of Methods
- LOC – Lines of Code
- CYCLO - Cyclomatic complexity
- CALLS - number of distinct function- and method-calls
- ANDC - Average Number of Derived Classes
- AHH - Average Hierarchy Height
nimayneb/yawl 适用场景与选型建议
nimayneb/yawl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 10 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「pattern」 「matcher」 「query」 「asterisk」 「string」 「token」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nimayneb/yawl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nimayneb/yawl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nimayneb/yawl 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generic engine to match identifiers against simple yet powerful patterns
AppVerk extension of PHP Matcher, it enables you to match values with patterns
Implementation of cookie path matching specified in RFC6265
For cookie domain and url comparisons
Query filtering in your frontend
Anax Database Active Record module for model classes.
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 9
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0
- 更新时间: 2019-10-14