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:31:55 -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 1444754054 31985 80.91.229.3 (13 Oct 2015 16:34:14 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 13 Oct 2015 16:34:14 +0000 (UTC) Cc: Help Gnu Emacs mailing list Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Oct 13 18:34:06 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 1Zm2Wb-0001Pt-K5 for geh-help-gnu-emacs@m.gmane.org; Tue, 13 Oct 2015 18:34:05 +0200 Original-Received: from localhost ([::1]:37330 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zm2Wa-0003Fp-Gz for geh-help-gnu-emacs@m.gmane.org; Tue, 13 Oct 2015 12:34:04 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41869) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zm2VD-0002Zt-Fn for help-gnu-emacs@gnu.org; Tue, 13 Oct 2015 12:32:40 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zm2V9-0003yc-Fq for help-gnu-emacs@gnu.org; Tue, 13 Oct 2015 12:32:39 -0400 Original-Received: from mail-oi0-x22b.google.com ([2607:f8b0:4003:c06::22b]:33445) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zm2V9-0003yV-B1 for help-gnu-emacs@gnu.org; Tue, 13 Oct 2015 12:32:35 -0400 Original-Received: by oiar126 with SMTP id r126so12768139oia.0 for ; Tue, 13 Oct 2015 09:32:35 -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:cc :content-type; bh=f89/OrmRrFdidUywJ4qYb1/DaPKCCl4hsx+s1iwt6QE=; b=qpZXgFQs/vQ7eUUlTgwd/kw5zSZBhOWPLrFYvBSyAK1A6TToxPAhtfFcC6JaAkws3Z 2RQtU4b8RibvQuESZ541pI4U/8ZHwpBLah3Ajm6+FCMT1C2cXU5Y4kcKeSA/2z/cgN0f mQ0fZ4zmp8NesooiQWBhzM+j6PlAooafjT2Yp1CEhZoSY5mrZVu/BfnthhTUe6+EIN6P n6QipTeSybxsPmyW07dqPwNcCfQtfkYDGqc1vEmzlSMTRdbEkK65logA5x+BWbNO/zNN EQa7ZVnCIfg1EtiZHoFE/gP7oqsBn6sCfTtqu4Bprn6NclYoYXuFaSuHRV5LZaP1tjBm +XkA== X-Received: by 10.202.211.10 with SMTP id k10mr19227586oig.34.1444753954899; Tue, 13 Oct 2015 09:32:34 -0700 (PDT) Original-Received: by 10.202.172.205 with HTTP; Tue, 13 Oct 2015 09:31:55 -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:c06::22b 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:107606 Archived-At: Sorry, I don't intend to keep on spamming this thread. But I made a dumb mistake in the previous code. Below is fixed. The while loop now stops as soon as match-pos is nil; there was no need to crawl through the whole string. (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+ (length str)))))) (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 ?l "falalalaabcds") (my/count-char-in-string ?^ "f^la^lala^dabra^^") (my/count-char-in-string ?\\ "\\falalala\\")) -- Kaushal Modi On Tue, Oct 13, 2015 at 12:06 PM, Kaushal Modi wrote: > 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 >> >>