问题描述
我在 AIX Box 上编译了 ruby 64 位.似乎没有任何问题,除非我在代码中使用了一些特定的正则表达式.下面是一个例子:
I have compiled ruby 64 bit on an AIX Box.There doesn't seem to be any issue except when I use some specific regular expressions in my code.Here is an example:
/([0-9]){1000}/.match("2")
结果:
RegexpError: too big quantifier in {,}: /([0-9]*){1000}/
当我尝试减少重复次数时,它似乎奏效了.
When I try reducing the number of repetitions, it seems to work.
我尝试深入研究 ruby 代码.但无法理解其中的原因.这是我们在 AIX/64 位 ruby 中的某种依赖或限制吗?
I tried digging into the ruby code. But could not understand the reason.Is this some dependency or restriction that we have in AIX/64 bit ruby?
提前致谢:)
推荐答案
我几乎立即找到了答案.
I almost immediately found the answer.
我做的第一件事是在 ruby 源代码中搜索抛出的错误.我发现 regex.h 对此负责.
The first thing I did was to search in the ruby source code for the error being thrown. I found that regex.h was responsible for this.
在regex.h中,代码流程是这样的:
In regex.h, the code flow is something like this:
/* Maximum number of duplicates an interval can allow. */
#ifndef RE_DUP_MAX
#define RE_DUP_MAX ((1 << 15) - 1)
#endif
现在这里的问题是 RE_DUP_MAX.在 AIX 机器上,在/usr/include 中的某处定义了相同的常量.我搜索它并在
Now the problem here is RE_DUP_MAX. On AIX box, the same constant has been defined somewhere in /usr/include. I searched for it and found in
/usr/include/NLregexp.h
/usr/include/sys/limits.h
/usr/include/unistd.h
我不确定正在使用这三个中的哪一个(很可能是 NLregexp.h).在这些标头中,RE_DUP_MAX 的值已设置为 255!所以正则表达式的重复次数是有上限的!
I am not sure which of the three is being used(most probably NLregexp.h). In these headers, the value of RE_DUP_MAX has been set to 255! So there is a cap placed on the number of repetitions of a regex!
简而言之,原因是编译采用了系统定义的值,而不是我们在regex.h中定义的值!
In short, the reason is the compilation taking the system defined value than that we define in regex.h!
因此通过在 regex.h 中重新分配 RE_DUP_MAX 的值解决了问题即
Hence the issue was solved by reassigning the value of RE_DUP_MAX in regex.hi.e
# ifdef RE_DUP_MAX
# undef RE_DUP_MAX
# endif
# define RE_DUP_MAX ((1 << 15) - 1)
干杯!
这篇关于ruby 64位aix编译中的正则表达式限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!