diff options
author | Galilée 'Bill' Enguehard <[email protected]> | 2020-05-21 22:27:38 +0100 |
---|---|---|
committer | Galilée 'Bill' Enguehard <[email protected]> | 2020-05-21 22:27:38 +0100 |
commit | 7fece3bdd2450c0807f7dd742239cae95f0cc65e (patch) | |
tree | 866c4db826c959e79c63a6727bdb9f2c61e6fc4f /crates/stdx | |
parent | db926218b2082077750291f8426ddd28b284cd08 (diff) | |
parent | 59732df8d40dfadc6dcf5951265416576399712a (diff) |
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer into modname_spacing
Diffstat (limited to 'crates/stdx')
-rw-r--r-- | crates/stdx/src/lib.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 01cdf452c..71a57fba2 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs | |||
@@ -102,3 +102,25 @@ pub fn timeit(label: &'static str) -> impl Drop { | |||
102 | 102 | ||
103 | Guard { label, start: Instant::now() } | 103 | Guard { label, start: Instant::now() } |
104 | } | 104 | } |
105 | |||
106 | pub fn to_lower_snake_case(s: &str) -> String { | ||
107 | let mut buf = String::with_capacity(s.len()); | ||
108 | let mut prev = false; | ||
109 | for c in s.chars() { | ||
110 | if c.is_ascii_uppercase() && prev { | ||
111 | buf.push('_') | ||
112 | } | ||
113 | prev = true; | ||
114 | |||
115 | buf.push(c.to_ascii_lowercase()); | ||
116 | } | ||
117 | buf | ||
118 | } | ||
119 | |||
120 | pub fn replace(buf: &mut String, from: char, to: &str) { | ||
121 | if !buf.contains(from) { | ||
122 | return; | ||
123 | } | ||
124 | // FIXME: do this in place. | ||
125 | *buf = buf.replace(from, to) | ||
126 | } | ||