From 8b8967de26d87453e0e92eb5c87a6d208b39ccb5 Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Thu, 12 Mar 2020 23:49:02 +0000 Subject: [PATCH] Simplify the `split_by_alphanumeric` function --- src/util.cr | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/src/util.cr b/src/util.cr index 86a3c2c..ede75a8 100644 --- a/src/util.cr +++ b/src/util.cr @@ -38,29 +38,10 @@ def is_numeric(str) end def split_by_alphanumeric(str) - is_number = false arr = [] of String - arr.push( - str.split("").reduce("") do |memo, c| - if (is_number) - if is_numeric(c) - memo + c - else - arr.push(memo) if memo != "" - is_number = false - c - end - else - if is_numeric(c) # number - arr.push(memo) if memo != "" - is_number = true - c - else - memo + c - end - end - end - ) + str.scan(/([^\d\n\r]*)(\d*)([^\d\n\r]*)/) do |match| + arr += match.captures.select{|s| s != ""} + end arr end