- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个用例,其中有一行包含嵌套标记的文本(例如 {
和 }
),并且我希望转换嵌套在特定深度的某些子字符串。
示例,在深度 1 处将单词 moo 大写:
moo [moo [moo moo]] moo ->
moo [MOO [moo moo]] moo
实现者:
replaceTokens(input, 1, "[", "]", "moo", String::toUpperCase);
或者现实世界的示例,提供尚未使用青色颜色序列着色的“--options”:
@|blue --ignoreLog|@ works, but --ignoreOutput silences everything. ->
@|blue --ignoreLog|@ works, but @|cyan --ignoreOutput|@ silences everything.
实现者:
replaceTokens(input, 0, "@|", "|@", "--\\w*", s -> format("@|cyan %s|@", s));
<小时/>
我已经实现了这个逻辑,虽然我感觉很好(可能除了性能),但我也觉得我重新发明了轮子。以下是我的实现方法:
set currentPos to zero
while (input line not fully consumed) {
take the remaining line
if the open token is matched, add to output, increase counter and advance pos accordingly
else if the close token is matched, add to output, decrease counter and advance pos accordingly
else if the counter matches provided depth and given regex matches, invoke replacer function and advance pos accordingly
else just record the next character and advance pos by 1
}
下面是实际的实现:
public static String replaceNestedTokens(String lineWithTokens, int nestingDepth, String tokenOpen, String tokenClose, String tokenRegexToReplace, Function<String, String> tokenReplacer) {
final Pattern startsWithOpen = compile(quote(tokenOpen));
final Pattern startsWithClose = compile(quote(tokenClose));
final Pattern startsWithTokenToReplace = compile(format("(?<token>%s)", tokenRegexToReplace));
final StringBuilder lineWithTokensReplaced = new StringBuilder();
int countOpenTokens = 0;
int pos = 0;
while (pos < lineWithTokens.length()) {
final String remainingLine = lineWithTokens.substring(pos);
if (startsWithOpen.matcher(remainingLine).lookingAt()) {
countOpenTokens++;
lineWithTokensReplaced.append(tokenOpen);
pos += tokenOpen.length();
} else if (startsWithClose.matcher(remainingLine).lookingAt()) {
countOpenTokens--;
lineWithTokensReplaced.append(tokenClose);
pos += tokenClose.length();
} else if (countOpenTokens == nestingDepth) {
Matcher startsWithTokenMatcher = startsWithTokenToReplace.matcher(remainingLine);
if (startsWithTokenMatcher.lookingAt()) {
String matchedToken = startsWithTokenMatcher.group("token");
lineWithTokensReplaced.append(tokenReplacer.apply(matchedToken));
pos += matchedToken.length();
} else {
lineWithTokensReplaced.append(lineWithTokens.charAt(pos++));
}
} else {
lineWithTokensReplaced.append(lineWithTokens.charAt(pos++));
}
assumeTrue(countOpenTokens >= 0, "Unbalanced token sets: closed token without open token\n\t" + lineWithTokens);
}
assumeTrue(countOpenTokens == 0, "Unbalanced token sets: open token without closed token\n\t" + lineWithTokens);
return lineWithTokensReplaced.toString();
}
我无法让它与像 this 这样的正则表达式一起工作或this (或扫描仪)解决方案,但我觉得我正在重新发明轮子,并且可以使用(普通 Java)开箱即用的类以更少的代码解决这个问题。另外,我很确定这对于所有内联模式/匹配器实例和子字符串来说都是一场性能噩梦。
建议?
最佳答案
您可以使用像 ANTLR 这样的解析器创建语法来描述您的语言或语法。然后使用监听器或访问者来制作 token 的解释器。
语法示例如下(我可以从您的代码中推断出):
grammar Expr;
prog: (expr NEWLINE)* ;
expr: id '[' expr ']'
| '@|' expr '|@'
| '--ignoreLog' expr
| '--ignoreOutput' expr
| string
;
string: [a-zA-Z0-9];
NEWLINE : [\r\n]+ ;
关于java - 我是否用这个 token 替换代码重新发明了轮子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52015571/
从下面的 setup.py 文件中,我试图从一个应该只包含 python 2.7 代码的项目中创建一个纯 python wheel。 from setuptools import setup setu
我正在尝试在 Linux 上安装 pytorch ( http://pytorch.org/ ),根据我的机器配置,我应该运行的命令是: pip 安装 https://s3.amazonaws.com
也许是一个广泛的问题,但我认为这与任何使用 github 并可以显着减少他们的工作量的 python 包维护者相关,所以希望这些权力能够让它站住脚。 基本上,在我看来: 为 python 构建和发布轮
我有一个包含 C 扩展的包,我想上传到 pypi:https://github.com/Erotemic/netharn 我使用命令 python setup.py bdist_wheel --py-
TLDR :裤子获取 OS X 特定的轮子,因为我正在 Mac 上开发。我怎样才能避免这种情况,或者指定我将部署到 Ubuntu? 全文 : 尝试使用 Pants 打包 Python 应用程序。到目前
我正在 Azure Pipelines 上运行测试(但同样的情况也适用于 Travis 和 Appveyor)。我有一个 Python 包,我们将其称为“计算器”,其中包含 cython 扩展。当我推
是否有(事实上的)标准 MIME 类型可用于 Python 轮包?我在 PEP 427 中看不到任何内容。 我认为 application/zip 可能是一个选项,但这似乎误导了我。 最佳答案 没有。
我一整天都在为 --find-links 苦苦挣扎,如果有人能在这里帮助我,我将非常感激。 我一直在使用python3.4进行开发,我添加的新功能之一使用Azure存储(最新版本),它需要加密,这需要
之前,我们仅将内部 pip 存储库用于源代码分发。展望 future ,我们还希望托管轮子来完成两件事: 为(本地)开发人员机器和 Alpine Docker 环境提供我们自己的代码 为没有 Alpi
我是一名优秀的程序员,十分优秀!