We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8b13b49 commit 433b856Copy full SHA for 433b856
diff-so-fancy
@@ -602,18 +602,13 @@ sub insert_reset_at_line_end {
602
}
603
604
# Count the number of a given char in a string
605
+# https://www.perturb.org/display/1010_Perl_Count_occurrences_of_substring.html
606
sub char_count {
- my ($needle,$str) = @_;
607
- my $len = length($str);
608
- my $ret = 0;
609
-
610
- for (my $i = 0; $i < $len; $i++) {
611
- my $found = substr($str,$i,1);
+ my ($needle, $haystack) = @_;
612
613
- if ($needle eq $found) { $ret++; }
614
- }
+ my $count = () = ($haystack =~ /$needle/g);
615
616
- return $ret;
+ return $count;
617
618
619
# Remove all ANSI codes from a string
0 commit comments