Skip to content

Commit be49547

Browse files
committed
Use split_once polyfill
1 parent 239dd50 commit be49547

File tree

5 files changed

+30
-22
lines changed

5 files changed

+30
-22
lines changed

crates/ra_project_model/src/cfg_flag.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::str::FromStr;
55

66
use ra_cfg::CfgOptions;
7-
use stdx::split_delim;
7+
use stdx::split_once;
88

99
#[derive(Clone, Eq, PartialEq, Debug)]
1010
pub enum CfgFlag {
@@ -15,7 +15,7 @@ pub enum CfgFlag {
1515
impl FromStr for CfgFlag {
1616
type Err = String;
1717
fn from_str(s: &str) -> Result<Self, Self::Err> {
18-
let res = match split_delim(s, '=') {
18+
let res = match split_once(s, '=') {
1919
Some((key, value)) => {
2020
if !(value.starts_with('"') && value.ends_with('"')) {
2121
return Err(format!("Invalid cfg ({:?}), value should be in quotes", s));

crates/rust-analyzer/src/cli/analysis_bench.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::{env, path::Path, str::FromStr, sync::Arc, time::Instant};
44

5-
use anyhow::{format_err, Result};
5+
use anyhow::{bail, format_err, Result};
66
use ra_db::{
77
salsa::{Database, Durability},
88
FileId,
@@ -30,19 +30,18 @@ pub struct Position {
3030
impl FromStr for Position {
3131
type Err = anyhow::Error;
3232
fn from_str(s: &str) -> Result<Self> {
33-
let (path_line, column) = rsplit_at_char(s, ':')?;
34-
let (path, line) = rsplit_at_char(path_line, ':')?;
35-
let path = env::current_dir().unwrap().join(path);
36-
let path = AbsPathBuf::assert(path);
37-
Ok(Position { path, line: line.parse()?, column: column.parse()? })
33+
let mut split = s.rsplitn(3, ':');
34+
match (split.next(), split.next(), split.next()) {
35+
(Some(column), Some(line), Some(path)) => {
36+
let path = env::current_dir().unwrap().join(path);
37+
let path = AbsPathBuf::assert(path);
38+
Ok(Position { path, line: line.parse()?, column: column.parse()? })
39+
}
40+
_ => bail!("position should be in file:line:column format: {:?}", s),
41+
}
3842
}
3943
}
4044

41-
fn rsplit_at_char(s: &str, c: char) -> Result<(&str, &str)> {
42-
let idx = s.rfind(c).ok_or_else(|| format_err!("no `{}` in {}", c, s))?;
43-
Ok((&s[..idx], &s[idx + 1..]))
44-
}
45-
4645
pub fn analysis_bench(
4746
verbosity: Verbosity,
4847
path: &Path,

crates/rust-analyzer/src/handlers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use ra_project_model::TargetKind;
2626
use ra_syntax::{algo, ast, AstNode, SyntaxKind, TextRange, TextSize};
2727
use serde::{Deserialize, Serialize};
2828
use serde_json::to_value;
29-
use stdx::{format_to, split_delim};
29+
use stdx::{format_to, split_once};
3030

3131
use crate::{
3232
cargo_target_spec::CargoTargetSpec,
@@ -865,7 +865,7 @@ pub(crate) fn handle_resolve_code_action(
865865
.map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
866866

867867
let assists = snap.analysis.resolved_assists(&snap.config.assist, frange)?;
868-
let (id_string, index) = split_delim(&params.id, ':').unwrap();
868+
let (id_string, index) = split_once(&params.id, ':').unwrap();
869869
let index = index.parse::<usize>().unwrap();
870870
let assist = &assists[index];
871871
assert!(assist.assist.id.0 == id_string);

crates/stdx/src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,18 @@ pub fn replace(buf: &mut String, from: char, to: &str) {
109109
*buf = buf.replace(from, to)
110110
}
111111

112-
pub fn split_delim(haystack: &str, delim: char) -> Option<(&str, &str)> {
113-
let idx = haystack.find(delim)?;
114-
Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..]))
112+
// https://github.com/rust-lang/rust/issues/74773
113+
pub fn split_once(haystack: &str, delim: char) -> Option<(&str, &str)> {
114+
let mut split = haystack.splitn(2, delim);
115+
let prefix = split.next()?;
116+
let suffix = split.next()?;
117+
Some((prefix, suffix))
118+
}
119+
pub fn rsplit_once(haystack: &str, delim: char) -> Option<(&str, &str)> {
120+
let mut split = haystack.rsplitn(2, delim);
121+
let suffix = split.next()?;
122+
let prefix = split.next()?;
123+
Some((prefix, suffix))
115124
}
116125

117126
pub fn trim_indent(mut text: &str) -> String {

crates/test_utils/src/fixture.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! rust-analyzer database from a single string.
33
44
use rustc_hash::FxHashMap;
5-
use stdx::{lines_with_ends, split_delim, trim_indent};
5+
use stdx::{lines_with_ends, split_once, trim_indent};
66

77
#[derive(Debug, Eq, PartialEq)]
88
pub struct Fixture {
@@ -71,22 +71,22 @@ impl Fixture {
7171
let mut cfg_key_values = Vec::new();
7272
let mut env = FxHashMap::default();
7373
for component in components[1..].iter() {
74-
let (key, value) = split_delim(component, ':').unwrap();
74+
let (key, value) = split_once(component, ':').unwrap();
7575
match key {
7676
"crate" => krate = Some(value.to_string()),
7777
"deps" => deps = value.split(',').map(|it| it.to_string()).collect(),
7878
"edition" => edition = Some(value.to_string()),
7979
"cfg" => {
8080
for entry in value.split(',') {
81-
match split_delim(entry, '=') {
81+
match split_once(entry, '=') {
8282
Some((k, v)) => cfg_key_values.push((k.to_string(), v.to_string())),
8383
None => cfg_atoms.push(entry.to_string()),
8484
}
8585
}
8686
}
8787
"env" => {
8888
for key in value.split(',') {
89-
if let Some((k, v)) = split_delim(key, '=') {
89+
if let Some((k, v)) = split_once(key, '=') {
9090
env.insert(k.into(), v.into());
9191
}
9292
}

0 commit comments

Comments
 (0)