mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-06-07 20:20:26 -03:00
Working onigmo wrapper, but onigmo doesn't support anchored regexes
This commit is contained in:
parent
d49d0969a9
commit
2a9e7fde0d
106
src/onigmo.cr
106
src/onigmo.cr
@ -1,88 +1,34 @@
|
|||||||
@[Link("onigmo")]
|
@[Link("onigmo")]
|
||||||
|
@[Link(ldflags: "#{__DIR__}/onigmo/onigwrap.o")]
|
||||||
|
|
||||||
lib Onigmo
|
lib Onigmo
|
||||||
type OnigOptionType = UInt32
|
type Regex = Pointer(Void)
|
||||||
type OnigCaseFoldType = UInt32
|
type Region = Pointer(Void)
|
||||||
type OnigDistance = LibC::SizeT
|
|
||||||
|
|
||||||
struct OnigRegex
|
fun create = onigwrap_create(pattern : LibC::Char*, len : UInt32,
|
||||||
p : LibC::UChar*
|
ignoreCase : Int32,
|
||||||
used : UInt32
|
multiline : Int32,
|
||||||
alloc : UInt32
|
dotall : Int32,
|
||||||
|
anchored : Int32) : Regex
|
||||||
|
fun free = onigwrap_free(re : Regex)
|
||||||
|
fun region_free = onigwrap_region_free(region : Region)
|
||||||
|
|
||||||
num_mem : Int32
|
fun search = onigwrap_search(re : Regex, str : LibC::Char*, offset : UInt32, length : UInt32) : Region
|
||||||
num_repeat : Int32
|
fun num_regs = onigwrap_num_regs(region : Region) : Int32
|
||||||
num_null_check : Int32
|
fun pos = onigwrap_pos(region : Region, index : Int32) : Int32
|
||||||
num_comb_exp_check : Int32
|
fun len = onigwrap_len(region : Region, index : Int32) : Int32
|
||||||
num_call : Int32
|
|
||||||
capture_history : UInt32
|
|
||||||
bt_mem_start : UInt32
|
|
||||||
bt_mem_end : UInt32
|
|
||||||
stack_pop_level : Int32
|
|
||||||
repeat_range_alloc : Int32
|
|
||||||
options : OnigOptionType
|
|
||||||
syntax : OnigSyntaxType*
|
|
||||||
name_table : Void*
|
|
||||||
case_fold_flag : OnigCaseFoldType
|
|
||||||
optimize : Int32
|
|
||||||
threshold_len : Int32
|
|
||||||
anchor : Int32
|
|
||||||
anchor_dmin : OnigDistance
|
|
||||||
anchor_dmax : OnigDistance
|
|
||||||
sub_anchor : Int32
|
|
||||||
exact : LibC::UChar*
|
|
||||||
exact_end : LibC::UChar*
|
|
||||||
map : LibC::UChar*
|
|
||||||
int_map : Int32*
|
|
||||||
int_map_backward : Int32*
|
|
||||||
dmin : OnigDistance
|
|
||||||
dmax : OnigDistance
|
|
||||||
chain : OnigRegex*
|
|
||||||
end
|
|
||||||
|
|
||||||
type OnigRegexType = OnigRegex*
|
|
||||||
type OnigCodePoint = UInt32
|
|
||||||
type OnigUChar = LibC::UChar
|
|
||||||
type OnigEncoding = Void*
|
|
||||||
|
|
||||||
struct OnigMetaCharTableType
|
|
||||||
esc : OnigCodePoint
|
|
||||||
anychar : OnigCodePoint
|
|
||||||
anytime : OnigCodePoint
|
|
||||||
zero_or_one_time : OnigCodePoint
|
|
||||||
one_or_one_time : OnigCodePoint
|
|
||||||
anychar_anytime : OnigCodePoint
|
|
||||||
end
|
|
||||||
|
|
||||||
struct OnigSyntaxType
|
|
||||||
op : UInt32
|
|
||||||
op2 : UInt32
|
|
||||||
behavior : UInt32
|
|
||||||
options : OnigOptionType
|
|
||||||
meta_char_table : OnigMetaCharTableType
|
|
||||||
end
|
|
||||||
|
|
||||||
struct OnigErrorInfo
|
|
||||||
enc : OnigEncoding
|
|
||||||
par : OnigUChar*
|
|
||||||
par_end : OnigUChar*
|
|
||||||
end
|
|
||||||
|
|
||||||
ONIG_OPTION_NONE = 0u32
|
|
||||||
ONIG_OPTION_DEFAULT = ONIG_OPTION_NONE
|
|
||||||
|
|
||||||
|
|
||||||
fun new = onig_new(OnigRegex*, OnigUChar*, OnigUChar*, OnigOptionType, OnigEncoding, OnigSyntaxType*, OnigErrorInfo*)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
pattern = "a(.*)b|[e-f]+"
|
pattern = "a"
|
||||||
str = "zzzzaffffffffb"
|
str = "# foobar"
|
||||||
|
|
||||||
einfo = Onigmo::OnigErrorInfo.new
|
re = Onigmo.create(pattern, pattern.size, false, true, false, false)
|
||||||
|
region = Onigmo.search(re, str.to_unsafe, 0, str.bytesize)
|
||||||
Onigmo.new(out reg,
|
num_regs = Onigmo.num_regs(region)
|
||||||
pattern.to_unsafe,
|
(0...num_regs).each do |i|
|
||||||
pattern.to_unsafe + pattern.size,
|
pos = Onigmo.pos(region, i)
|
||||||
Onigmo::ONIG_OPTION_DEFAULT,
|
len = Onigmo.len(region, i)
|
||||||
0,
|
puts "match #{i}: #{str[pos, len]}"
|
||||||
Onigmo::ONIG_SYNTAX_DEFAULT, pointerof(einfo))
|
end
|
||||||
|
Onigmo.region_free(region)
|
||||||
|
Onigmo.free(re)
|
||||||
|
94
src/onigmo/onigwrap.c
Normal file
94
src/onigmo/onigwrap.c
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
#include "onigmo.h"
|
||||||
|
|
||||||
|
regex_t *onigwrap_create(char *pattern, int len, int ignoreCase, int multiline, int dotall, int anchored )
|
||||||
|
{
|
||||||
|
regex_t *reg;
|
||||||
|
|
||||||
|
OnigErrorInfo einfo;
|
||||||
|
|
||||||
|
OnigOptionType onigOptions = ONIG_OPTION_DEFAULT;
|
||||||
|
|
||||||
|
if (ignoreCase == 1)
|
||||||
|
onigOptions |= ONIG_OPTION_IGNORECASE;
|
||||||
|
|
||||||
|
if (multiline == 1)
|
||||||
|
onigOptions |= ONIG_OPTION_NEGATE_SINGLELINE;
|
||||||
|
|
||||||
|
if (dotall == 1)
|
||||||
|
onigOptions |= ONIG_OPTION_DOTALL;
|
||||||
|
|
||||||
|
OnigUChar *stringStart = (OnigUChar*) pattern;
|
||||||
|
OnigUChar *stringEnd = (OnigUChar*) pattern + len;
|
||||||
|
int res = onig_new(®, stringStart, stringEnd, onigOptions, ONIG_ENCODING_UTF8, ONIG_SYNTAX_PYTHON, &einfo);
|
||||||
|
|
||||||
|
return reg;
|
||||||
|
}
|
||||||
|
|
||||||
|
void onigwrap_region_free(OnigRegion *region)
|
||||||
|
{
|
||||||
|
onig_region_free(region, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onigwrap_free(regex_t *reg)
|
||||||
|
{
|
||||||
|
onig_free(reg);
|
||||||
|
}
|
||||||
|
|
||||||
|
int onigwrap_index_in(regex_t *reg, char *charPtr, int offset, int length)
|
||||||
|
{
|
||||||
|
OnigUChar *stringStart = (OnigUChar*) charPtr;
|
||||||
|
OnigUChar *stringEnd = (OnigUChar*) (charPtr + length);
|
||||||
|
OnigUChar *stringOffset = (OnigUChar*) (charPtr + offset);
|
||||||
|
OnigUChar *stringRange = (OnigUChar*) stringEnd;
|
||||||
|
|
||||||
|
OnigRegion *region = onig_region_new();
|
||||||
|
int result = onig_search(reg, stringStart, stringEnd, stringOffset, stringRange, region, ONIG_OPTION_NONE);
|
||||||
|
onig_region_free(region, 1);
|
||||||
|
|
||||||
|
if (result >= 0)
|
||||||
|
return result >> 1;
|
||||||
|
if (result == ONIG_MISMATCH)
|
||||||
|
return -1;
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
OnigRegion *onigwrap_search(regex_t *reg, char *charPtr, int offset, int length)
|
||||||
|
{
|
||||||
|
OnigUChar *stringStart = (OnigUChar*) charPtr;
|
||||||
|
OnigUChar *stringEnd = (OnigUChar*) (charPtr + length);
|
||||||
|
OnigUChar *stringOffset = (OnigUChar*) (charPtr + offset);
|
||||||
|
OnigUChar *stringRange = (OnigUChar*) stringEnd;
|
||||||
|
|
||||||
|
OnigRegion *region = onig_region_new();
|
||||||
|
|
||||||
|
int result = onig_search(reg, stringStart, stringEnd, stringOffset, stringRange, region, ONIG_OPTION_NONE);
|
||||||
|
return region;
|
||||||
|
}
|
||||||
|
|
||||||
|
int onigwrap_num_regs(OnigRegion *region)
|
||||||
|
{
|
||||||
|
return region->num_regs;
|
||||||
|
}
|
||||||
|
|
||||||
|
int onigwrap_pos(OnigRegion *region, int nth)
|
||||||
|
{
|
||||||
|
if (nth < region->num_regs)
|
||||||
|
{
|
||||||
|
int result = region->beg[nth];
|
||||||
|
if (result < 0)
|
||||||
|
return -1;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int onigwrap_len(OnigRegion *region, int nth)
|
||||||
|
{
|
||||||
|
if (nth < region->num_regs)
|
||||||
|
{
|
||||||
|
int result = region->end[nth] - region->beg[nth];
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
32
src/onigmo/onigwrap.h
Normal file
32
src/onigmo/onigwrap.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#include "oniguruma.h"
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
#define ONIGWRAP_EXTERN extern __declspec(dllexport)
|
||||||
|
#else
|
||||||
|
#define ONIGWRAP_EXTERN extern
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ONIGWRAP_EXTERN
|
||||||
|
regex_t *onigwrap_create(char *pattern, int len, int ignoreCase, int multiline);
|
||||||
|
|
||||||
|
ONIGWRAP_EXTERN
|
||||||
|
void onigwrap_region_free(OnigRegion *region);
|
||||||
|
|
||||||
|
ONIGWRAP_EXTERN
|
||||||
|
void onigwrap_free(regex_t *reg);
|
||||||
|
|
||||||
|
ONIGWRAP_EXTERN
|
||||||
|
int onigwrap_index_in(regex_t *reg, char *charPtr, int offset, int length);
|
||||||
|
|
||||||
|
ONIGWRAP_EXTERN
|
||||||
|
OnigRegion *onigwrap_search(regex_t *reg, char *charPtr, int offset, int length);
|
||||||
|
|
||||||
|
ONIGWRAP_EXTERN
|
||||||
|
int onigwrap_num_regs(OnigRegion *region);
|
||||||
|
|
||||||
|
ONIGWRAP_EXTERN
|
||||||
|
int onigwrap_pos(OnigRegion *region, int nth);
|
||||||
|
|
||||||
|
ONIGWRAP_EXTERN
|
||||||
|
int onigwrap_len(OnigRegion *region, int nth);
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user