MCPcopy Index your code
hub / github.com/RustPython/RustPython / template

Function template

crates/vm/src/stdlib/_sre.rs:140–165  ·  view source on GitHub ↗
(
        _pattern: PyObjectRef,
        template: PyListRef,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

138
139 #[pyfunction]
140 fn template(
141 _pattern: PyObjectRef,
142 template: PyListRef,
143 vm: &VirtualMachine,
144 ) -> PyResult<Template> {
145 let err = || vm.new_type_error("invalid template");
146
147 let mut items = Vec::with_capacity(1);
148 let v = template.borrow_vec();
149 let literal = v.first().ok_or_else(err)?.clone();
150 let trunks = v[1..].chunks_exact(2);
151
152 if !trunks.remainder().is_empty() {
153 return Err(err());
154 }
155
156 for trunk in trunks {
157 let index: usize = trunk[0]
158 .downcast_ref::<PyInt>()
159 .ok_or_else(|| vm.new_type_error("expected usize"))?
160 .try_to_primitive(vm)?;
161 items.push((index, trunk[1].clone()));
162 }
163
164 Ok(Template { literal, items })
165 }
166
167 #[derive(FromArgs)]
168 struct StringArgs {

Callers

nothing calls this directly

Calls 9

borrow_vecMethod · 0.80
ok_or_elseMethod · 0.80
remainderMethod · 0.80
try_to_primitiveMethod · 0.80
ErrClass · 0.50
cloneMethod · 0.45
firstMethod · 0.45
is_emptyMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected