From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Kaushal Modi Newsgroups: gmane.emacs.help Subject: Re: How to count the number of occurrences of a character in a string? Date: Tue, 13 Oct 2015 12:06:36 -0400 Message-ID: References: <874mhvq3b2.fsf@mbork.pl> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: ger.gmane.org 1444752503 923 80.91.229.3 (13 Oct 2015 16:08:23 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 13 Oct 2015 16:08:23 +0000 (UTC) Cc: Help Gnu Emacs mailing list To: Stefan Monnier Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Oct 13 18:08:23 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Zm27j-0000dW-2m for geh-help-gnu-emacs@m.gmane.org; Tue, 13 Oct 2015 18:08:23 +0200 Original-Received: from localhost ([::1]:37203 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zm27i-0004Rk-FL for geh-help-gnu-emacs@m.gmane.org; Tue, 13 Oct 2015 12:08:22 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:33042) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zm26h-0004A0-Tu for help-gnu-emacs@gnu.org; Tue, 13 Oct 2015 12:07:20 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zm26e-0002fl-M0 for help-gnu-emacs@gnu.org; Tue, 13 Oct 2015 12:07:17 -0400 Original-Received: from mail-ob0-x235.google.com ([2607:f8b0:4003:c01::235]:35982) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zm26e-0002fa-HM for help-gnu-emacs@gnu.org; Tue, 13 Oct 2015 12:07:16 -0400 Original-Received: by obbrx8 with SMTP id rx8so17452836obb.3 for ; Tue, 13 Oct 2015 09:07:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=0zL28TaTYnwqN8ZqnYp6Vl2kuoA0bNLBEoxb3c16W+A=; b=iPlHp3eB3frXQVE7/GsLiQsGWuWaNX+cQcKUuBzSVD1b/ub3nTHHGW4f6xidymMSfj mkqGe++G2EgjJ4BufzwC0DFNcnZ9Bjpe+U7rny9YjVbMInECWN7yG09Emm27x0Z+QWJ0 yARg6+Ih5W33IdoxcYzXmR0EuI91zOaSxBzq20rf3UdEfDbcxlAVY7I2xx0dRHVaxxct i5ZfLtI44ZmphDm4Vs1o5WeEEhRfsTVfzhYZOW9fdAyMe9CGZJ9xmXkBd6QembUWo6Lr bSgg2b5Ssmtfd627G+VJFjmaCcVGP16uNuAhDGkhmGVYXJDwJtE/je/NULwYvWwTEvSx Mdtw== X-Received: by 10.182.126.169 with SMTP id mz9mr7544951obb.63.1444752436034; Tue, 13 Oct 2015 09:07:16 -0700 (PDT) Original-Received: by 10.202.172.205 with HTTP; Tue, 13 Oct 2015 09:06:36 -0700 (PDT) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4003:c01::235 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:107603 Archived-At: Thanks Nick, Stefan for the feedback. Here's the updated code just to fix the issues you pointed out (with a little "test-suite" at the end of that progn form) : (progn (defun my/count-char-in-string (char str) "Count the number of times CHAR character appears in STR string." (message "\n==========\nstr = %0s" str) (let* ((num-matches 0) (ptr 0) ; initiate pointer for string match match-pos) (while (<= ptr (length str)) (message "ptr = %0d" ptr) (setq match-pos (string-match-p (regexp-quote (char-to-string char)) str ptr)) (if match-pos (progn (setq ptr (1+ match-pos)) (message "match-pos = %0d ptr = %0d" match-pos ptr) (setq num-matches (1+ num-matches))) (progn (setq ptr (1+ ptr))))) (message "%0d occurrence%0s of `%c' char found in \"%s\"." num-matches (if (/= 1 num-matches) "s" "") char str) num-matches)) (my/count-char-in-string ?a "abcda") (my/count-char-in-string ?z "abcda") (my/count-char-in-string ?f "falalala") (my/count-char-in-string ?^ "f^la^lala^dabra^^") (my/count-char-in-string ?\\ "\\falalala\\")) -- Kaushal Modi On Mon, Oct 12, 2015 at 9:18 PM, Stefan Monnier wrote: >> (setq str (substring-no-properties str (1+ match-pos))) > > This will give wrong results for regexp that use things like ^ and \`. > > Instead, you should keep using the same `str' and instead pass the > `start' arg to string-match. > > > Stefan > >