Skip to content

Commit ad76f3c

Browse files
committed
Add Empty variant for NamedGroups to avoid cloning empty Arc<HashMap>
1 parent ca6dd78 commit ad76f3c

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/re.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ impl<'r, 't> Iterator for RegexSplitsN<'r, 't> {
811811
}
812812

813813
enum NamedGroups {
814+
Empty,
814815
Native(&'static [(&'static str, usize)]),
815816
Dynamic(Arc<HashMap<String, usize>>),
816817
}
@@ -821,12 +822,17 @@ impl NamedGroups {
821822
Regex::Native(ExNative { ref groups, .. }) =>
822823
NamedGroups::Native(groups),
823824
Regex::Dynamic(ref exec) =>
824-
NamedGroups::Dynamic(exec.named_groups().clone())
825+
if exec.named_groups().is_empty() {
826+
NamedGroups::Empty
827+
} else {
828+
NamedGroups::Dynamic(exec.named_groups().clone())
829+
}
825830
}
826831
}
827832

828833
fn pos(&self, name: &str) -> Option<usize> {
829834
match *self {
835+
NamedGroups::Empty => None,
830836
NamedGroups::Native(groups) => {
831837
groups.binary_search_by(|&(n, _)| n.cmp(name))
832838
.ok().map(|i| groups[i].1)
@@ -839,13 +845,15 @@ impl NamedGroups {
839845

840846
fn iter<'n>(&'n self) -> NamedGroupsIter<'n> {
841847
match *self {
848+
NamedGroups::Empty => NamedGroupsIter::Empty,
842849
NamedGroups::Native(g) => NamedGroupsIter::Native(g.iter()),
843850
NamedGroups::Dynamic(ref g) => NamedGroupsIter::Dynamic(g.iter()),
844851
}
845852
}
846853
}
847854

848855
enum NamedGroupsIter<'n> {
856+
Empty,
849857
Native(::std::slice::Iter<'static, (&'static str, usize)>),
850858
Dynamic(::std::collections::hash_map::Iter<'n, String, usize>),
851859
}
@@ -855,6 +863,8 @@ impl<'n> Iterator for NamedGroupsIter<'n> {
855863

856864
fn next(&mut self) -> Option<Self::Item> {
857865
match *self {
866+
NamedGroupsIter::Empty =>
867+
None,
858868
NamedGroupsIter::Native(ref mut it) =>
859869
it.next().map(|&v| v),
860870
NamedGroupsIter::Dynamic(ref mut it) =>

0 commit comments

Comments
 (0)