Server IP : 85.214.239.14 / Your IP : 18.218.1.38 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/2/task/2/root/proc/2/root/usr/share/doc/re2c/examples/rust/submatch/ |
Upload File : |
// re2rust $INPUT -o $OUTPUT // Maximum number of capturing groups among all rules. /*!maxnmatch:re2c*/ #[derive(Debug, PartialEq)] struct SemVer(u32, u32, u32); // version: (major, minor, patch) fn s2n(str: &[u8]) -> u32 { // convert a pre-parsed string to a number let mut n = 0; for i in str { n = n * 10 + *i as u32 - 48; } return n; } fn parse(str: &[u8]) -> Option<SemVer> { let (mut cur, mut mar) = (0, 0); // Allocate memory for capturing parentheses (twice the number of groups). let yynmatch: usize; let mut yypmatch = [0; YYMAXNMATCH*2]; // Autogenerated tag variables used by the lexer to track tag values. const NONE: usize = std::usize::MAX; /*!stags:re2c format = 'let mut @@{tag} = NONE;'; */ /*!re2c re2c:define:YYCTYPE = u8; re2c:define:YYPEEK = "*str.get_unchecked(cur)"; re2c:define:YYSKIP = "cur += 1;"; re2c:define:YYBACKUP = "mar = cur;"; re2c:define:YYRESTORE = "cur = mar;"; re2c:define:YYSTAGP = "@@{tag} = cur;"; re2c:define:YYSTAGN = "@@{tag} = NONE;"; re2c:define:YYSHIFTSTAG = "@@{tag} -= -@@{shift}isize as usize;"; re2c:yyfill:enable = 0; re2c:posix-captures = 1; num = [0-9]+; (num) "." (num) ("." num)? [\x00] { // `yynmatch` is the number of capturing groups assert_eq!(yynmatch, 4); // Even `yypmatch` values are for opening parentheses, odd values // are for closing parentheses, the first group is the whole match. let major = s2n(&str[yypmatch[2]..yypmatch[3]]); let minor = s2n(&str[yypmatch[4]..yypmatch[5]]); let patch = if yypmatch[6] == NONE {0} else {s2n(&str[yypmatch[6] + 1..yypmatch[7]])}; return Some(SemVer(major, minor, patch)); } * { return None; } */ } fn main() { assert_eq!(parse(b"23.34\0"), Some(SemVer(23, 34, 0))); assert_eq!(parse(b"1.2.99999\0"), Some(SemVer(1, 2, 99999))); assert_eq!(parse(b"1.a\0"), None); }