fn incrementString(string: &str) -> String { if string.is_empty() { return "1".to_string(); } let split_point = string .rfind(|ch: char| !ch.is_ascii_digit()) .map(|i| i + 1) .unwrap_or(0); let (text, numeric) = string.split_at(split_point); let next = numeric .parse() .map(|n: u128| n + 1) .unwrap_or(1); format!("{text}{next:>0width$}", width=numeric.len()) }
function incrementString (strng) {// return incrementedString- fn incrementString(string: &str) -> String {
- if string.is_empty() {
- return "1".to_string();
- }
- let split_point = string
- .rfind(|ch: char| !ch.is_ascii_digit())
- .map(|i| i + 1)
- .unwrap_or(0);
- let (text, numeric) = string.split_at(split_point);
- let next = numeric
- .parse()
- .map(|n: u128| n + 1)
- .unwrap_or(1);
- format!("{text}{next:>0width$}", width=numeric.len())
- }
#[cfg(test)] mod tests { use super::*; #[test] fn foobar000_foobar001() { assert_eq!(incrementString("foobar000"), "foobar001"); } #[test] fn foo_foo1() { assert_eq!(incrementString("foo"), "foo1"); } #[test] fn foobar001_foobar002() { assert_eq!(incrementString("foobar001"), "foobar002"); } #[test] fn foobar99_foobar100() { assert_eq!(incrementString("foobar99"), "foobar100"); } #[test] fn foobar099_foobar100() { assert_eq!(incrementString("foobar099"), "foobar100"); } #[test] fn from_empty_to_1() { assert_eq!(incrementString(""), "1"); } #[test] fn from_1_to_2() { assert_eq!(incrementString("1"), "2"); } }
describe("Tests", () => {it("test", () => {Test.assertEquals(incrementString("foobar000"), "foobar001");Test.assertEquals(incrementString("foo"), "foo1");Test.assertEquals(incrementString("foobar001"), "foobar002");Test.assertEquals(incrementString("foobar99"), "foobar100");Test.assertEquals(incrementString("foobar099"), "foobar100");Test.assertEquals(incrementString(""), "1");});});- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn foobar000_foobar001() {
- assert_eq!(incrementString("foobar000"), "foobar001");
- }
- #[test]
- fn foo_foo1() {
- assert_eq!(incrementString("foo"), "foo1");
- }
- #[test]
- fn foobar001_foobar002() {
- assert_eq!(incrementString("foobar001"), "foobar002");
- }
- #[test]
- fn foobar99_foobar100() {
- assert_eq!(incrementString("foobar99"), "foobar100");
- }
- #[test]
- fn foobar099_foobar100() {
- assert_eq!(incrementString("foobar099"), "foobar100");
- }
- #[test]
- fn from_empty_to_1() {
- assert_eq!(incrementString(""), "1");
- }
- #[test]
- fn from_1_to_2() {
- assert_eq!(incrementString("1"), "2");
- }
- }
Changes:
-
any
returns a bool based on a predicate, so removedmatch
andposition
- the
into_iter
wasn't needed becausechars
is already anIterator
!