forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
104 lines (99 loc) · 16.4 KB
/
Copy pathindex.html
File metadata and controls
104 lines (99 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Match regular expressions on arbitrary bytes."><meta name="keywords" content="rust, rustlang, rust-lang, bytes"><title>regex::bytes - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></head><body class="rustdoc mod"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../regex/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div>
</a><h2 class="location"></h2>
</nav>
<nav class="sidebar"><a class="sidebar-logo" href="../../regex/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div>
</a><h2 class="location"><a href="#">Module bytes</a></h2><div class="sidebar-elems"><section><div class="block"><ul><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li></ul></div></section></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../regex/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1">
<a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div>
</div></form></nav></div><section id="main-content" class="content"><div class="main-heading">
<h1 class="fqn"><span class="in-band">Module <a href="../index.html">regex</a>::<wbr><a class="mod" href="#">bytes</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span></h1><span class="out-of-band"><a class="srclink" href="../../src/regex/lib.rs.html#726">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Match regular expressions on arbitrary bytes.</p>
<p>This module provides a nearly identical API to the one found in the
top-level of this crate. There are two important differences:</p>
<ol>
<li>Matching is done on <code>&[u8]</code> instead of <code>&str</code>. Additionally, <code>Vec<u8></code>
is used where <code>String</code> would have been used.</li>
<li>Unicode support can be disabled even when disabling it would result in
matching invalid UTF-8 bytes.</li>
</ol>
<h2 id="example-match-null-terminated-string"><a href="#example-match-null-terminated-string">Example: match null terminated string</a></h2>
<p>This shows how to find all null-terminated strings in a slice of bytes:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let</span> <span class="ident">re</span> <span class="op">=</span> <span class="ident">Regex::new</span>(<span class="string">r"(?-u)(?P<cstr>[^\x00]+)\x00"</span>).<span class="ident">unwrap</span>();
<span class="kw">let</span> <span class="ident">text</span> <span class="op">=</span> <span class="string">b"foo\x00bar\x00baz\x00"</span>;
<span class="comment">// Extract all of the strings without the null terminator from each match.</span>
<span class="comment">// The unwrap is OK here since a match requires the `cstr` capture to match.</span>
<span class="kw">let</span> <span class="ident">cstrs</span>: <span class="ident">Vec</span><span class="op"><</span><span class="kw-2">&</span>[<span class="ident">u8</span>]<span class="op">></span> <span class="op">=</span>
<span class="ident">re</span>.<span class="ident">captures_iter</span>(<span class="ident">text</span>)
.<span class="ident">map</span>(<span class="op">|</span><span class="ident">c</span><span class="op">|</span> <span class="ident">c</span>.<span class="ident">name</span>(<span class="string">"cstr"</span>).<span class="ident">unwrap</span>().<span class="ident">as_bytes</span>())
.<span class="ident">collect</span>();
<span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[<span class="kw-2">&</span><span class="string">b"foo"</span>[..], <span class="kw-2">&</span><span class="string">b"bar"</span>[..], <span class="kw-2">&</span><span class="string">b"baz"</span>[..]], <span class="ident">cstrs</span>);</code></pre></div>
<h2 id="example-selectively-enable-unicode-support"><a href="#example-selectively-enable-unicode-support">Example: selectively enable Unicode support</a></h2>
<p>This shows how to match an arbitrary byte pattern followed by a UTF-8 encoded
string (e.g., to extract a title from a Matroska file):</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let</span> <span class="ident">re</span> <span class="op">=</span> <span class="ident">Regex::new</span>(
<span class="string">r"(?-u)\x7b\xa9(?:[\x80-\xfe]|[\x40-\xff].)(?u:(.*))"</span>
).<span class="ident">unwrap</span>();
<span class="kw">let</span> <span class="ident">text</span> <span class="op">=</span> <span class="string">b"\x12\xd0\x3b\x5f\x7b\xa9\x85\xe2\x98\x83\x80\x98\x54\x76\x68\x65"</span>;
<span class="kw">let</span> <span class="ident">caps</span> <span class="op">=</span> <span class="ident">re</span>.<span class="ident">captures</span>(<span class="ident">text</span>).<span class="ident">unwrap</span>();
<span class="comment">// Notice that despite the `.*` at the end, it will only match valid UTF-8</span>
<span class="comment">// because Unicode mode was enabled with the `u` flag. Without the `u` flag,</span>
<span class="comment">// the `.*` would match the rest of the bytes.</span>
<span class="kw">let</span> <span class="ident">mat</span> <span class="op">=</span> <span class="ident">caps</span>.<span class="ident">get</span>(<span class="number">1</span>).<span class="ident">unwrap</span>();
<span class="macro">assert_eq!</span>((<span class="number">7</span>, <span class="number">10</span>), (<span class="ident">mat</span>.<span class="ident">start</span>(), <span class="ident">mat</span>.<span class="ident">end</span>()));
<span class="comment">// If there was a match, Unicode mode guarantees that `title` is valid UTF-8.</span>
<span class="kw">let</span> <span class="ident">title</span> <span class="op">=</span> <span class="ident">str::from_utf8</span>(<span class="kw-2">&</span><span class="ident">caps</span>[<span class="number">1</span>]).<span class="ident">unwrap</span>();
<span class="macro">assert_eq!</span>(<span class="string">"☃"</span>, <span class="ident">title</span>);</code></pre></div>
<p>In general, if the Unicode flag is enabled in a capture group and that capture
is part of the overall match, then the capture is <em>guaranteed</em> to be valid
UTF-8.</p>
<h2 id="syntax"><a href="#syntax">Syntax</a></h2>
<p>The supported syntax is pretty much the same as the syntax for Unicode
regular expressions with a few changes that make sense for matching arbitrary
bytes:</p>
<ol>
<li>The <code>u</code> flag can be disabled even when disabling it might cause the regex to
match invalid UTF-8. When the <code>u</code> flag is disabled, the regex is said to be in
“ASCII compatible” mode.</li>
<li>In ASCII compatible mode, neither Unicode scalar values nor Unicode
character classes are allowed.</li>
<li>In ASCII compatible mode, Perl character classes (<code>\w</code>, <code>\d</code> and <code>\s</code>)
revert to their typical ASCII definition. <code>\w</code> maps to <code>[[:word:]]</code>, <code>\d</code> maps
to <code>[[:digit:]]</code> and <code>\s</code> maps to <code>[[:space:]]</code>.</li>
<li>In ASCII compatible mode, word boundaries use the ASCII compatible <code>\w</code> to
determine whether a byte is a word byte or not.</li>
<li>Hexadecimal notation can be used to specify arbitrary bytes instead of
Unicode codepoints. For example, in ASCII compatible mode, <code>\xFF</code> matches the
literal byte <code>\xFF</code>, while in Unicode mode, <code>\xFF</code> is a Unicode codepoint that
matches its UTF-8 encoding of <code>\xC3\xBF</code>. Similarly for octal notation when
enabled.</li>
<li>In ASCII compatible mode, <code>.</code> matches any <em>byte</em> except for <code>\n</code>. When the
<code>s</code> flag is additionally enabled, <code>.</code> matches any byte.</li>
</ol>
<h2 id="performance"><a href="#performance">Performance</a></h2>
<p>In general, one should expect performance on <code>&[u8]</code> to be roughly similar to
performance on <code>&str</code>.</p>
</div></details><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2>
<div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.CaptureLocations.html" title="regex::bytes::CaptureLocations struct">CaptureLocations</a></div><div class="item-right docblock-short"><p>CaptureLocations is a low level representation of the raw offsets of each
submatch.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.CaptureMatches.html" title="regex::bytes::CaptureMatches struct">CaptureMatches</a></div><div class="item-right docblock-short"><p>An iterator that yields all non-overlapping capture groups matching a
particular regular expression.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.CaptureNames.html" title="regex::bytes::CaptureNames struct">CaptureNames</a></div><div class="item-right docblock-short"><p>An iterator over the names of all possible captures.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Captures.html" title="regex::bytes::Captures struct">Captures</a></div><div class="item-right docblock-short"><p>Captures represents a group of captured byte strings for a single match.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Match.html" title="regex::bytes::Match struct">Match</a></div><div class="item-right docblock-short"><p>Match represents a single match of a regex in a haystack.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Matches.html" title="regex::bytes::Matches struct">Matches</a></div><div class="item-right docblock-short"><p>An iterator over all non-overlapping matches for a particular string.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.NoExpand.html" title="regex::bytes::NoExpand struct">NoExpand</a></div><div class="item-right docblock-short"><p><code>NoExpand</code> indicates literal byte string replacement.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Regex.html" title="regex::bytes::Regex struct">Regex</a></div><div class="item-right docblock-short"><p>A compiled regular expression for matching arbitrary bytes.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.RegexBuilder.html" title="regex::bytes::RegexBuilder struct">RegexBuilder</a></div><div class="item-right docblock-short"><p>A configurable builder for a regular expression.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.RegexSet.html" title="regex::bytes::RegexSet struct">RegexSet</a></div><div class="item-right docblock-short"><p>Match multiple (possibly overlapping) regular expressions in a single scan.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.RegexSetBuilder.html" title="regex::bytes::RegexSetBuilder struct">RegexSetBuilder</a></div><div class="item-right docblock-short"><p>A configurable builder for a set of regular expressions.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.ReplacerRef.html" title="regex::bytes::ReplacerRef struct">ReplacerRef</a></div><div class="item-right docblock-short"><p>By-reference adaptor for a <code>Replacer</code></p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.SetMatches.html" title="regex::bytes::SetMatches struct">SetMatches</a></div><div class="item-right docblock-short"><p>A set of matches returned by a regex set.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.SetMatchesIntoIter.html" title="regex::bytes::SetMatchesIntoIter struct">SetMatchesIntoIter</a></div><div class="item-right docblock-short"><p>An owned iterator over the set of matches from a regex set.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.SetMatchesIter.html" title="regex::bytes::SetMatchesIter struct">SetMatchesIter</a></div><div class="item-right docblock-short"><p>A borrowed iterator over the set of matches from a regex set.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Split.html" title="regex::bytes::Split struct">Split</a></div><div class="item-right docblock-short"><p>Yields all substrings delimited by a regular expression match.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.SplitN.html" title="regex::bytes::SplitN struct">SplitN</a></div><div class="item-right docblock-short"><p>Yields at most <code>N</code> substrings delimited by a regular expression match.</p>
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.SubCaptureMatches.html" title="regex::bytes::SubCaptureMatches struct">SubCaptureMatches</a></div><div class="item-right docblock-short"><p>An iterator that yields all capturing matches in the order in which they
appear in the regex.</p>
</div></div></div><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2>
<div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.Replacer.html" title="regex::bytes::Replacer trait">Replacer</a></div><div class="item-right docblock-short"><p>Replacer describes types that can be used to replace matches in a byte
string.</p>
</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="regex" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0-nightly (495b21669 2022-07-03)" ></div>
</body></html>