all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Kaushal Modi <kaushal.modi@gmail.com>
Cc: Help Gnu Emacs mailing list <help-gnu-emacs@gnu.org>
Subject: Re: How to count the number of occurrences of a character in a string?
Date: Tue, 13 Oct 2015 12:31:55 -0400	[thread overview]
Message-ID: <CAFyQvY047RqrJ+YNtOBcXTb0NFGrqLW10G9eSfYHChgJ9y2_qA@mail.gmail.com> (raw)
In-Reply-To: <CAFyQvY3iu8y_aDsn6HK5YD9n2W8G90PY+mZwp9YJC52vwJ-xSQ@mail.gmail.com>

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 <kaushal.modi@gmail.com> 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
> <monnier@iro.umontreal.ca> 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
>>
>>



  reply	other threads:[~2015-10-13 16:31 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-12 19:45 How to count the number of occurrences of a character in a string? Marcin Borkowski
2015-10-12 21:43 ` Kaushal Modi
2015-10-12 23:27   ` Nick Dokos
2015-10-13  1:18   ` Stefan Monnier
2015-10-13 16:06     ` Kaushal Modi
2015-10-13 16:31       ` Kaushal Modi [this message]
2015-10-13 16:48       ` Eli Zaretskii
2015-10-13 17:41         ` Eli Zaretskii
2015-10-13 17:43         ` Kaushal Modi
2015-10-13 19:07           ` Eli Zaretskii
2015-10-13 19:56             ` Kaushal Modi
2015-10-13 20:17               ` Eli Zaretskii
2015-10-13 20:41                 ` Kaushal Modi
     [not found]               ` <mailman.267.1444767491.7904.help-gnu-emacs@gnu.org>
2015-10-13 20:46                 ` Brendan Halpin
2015-10-13 21:07                   ` Kaushal Modi
     [not found]                   ` <mailman.273.1444770543.7904.help-gnu-emacs@gnu.org>
2015-10-13 22:44                     ` Joost Kremers
2015-10-14 10:04                       ` Oleh Krehel
2015-10-14 17:06                         ` Eli Zaretskii
2015-10-13 20:43           ` Michael Heerdegen
2015-10-13 20:59             ` Charles Curley
2015-10-14 18:19           ` Noam Postavsky
2015-10-14 20:00             ` Kaushal Modi
2015-10-13 22:00 ` Emanuel Berg
     [not found] <mailman.164.1444679125.7904.help-gnu-emacs@gnu.org>
2015-10-12 23:35 ` Joost Kremers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAFyQvY047RqrJ+YNtOBcXTb0NFGrqLW10G9eSfYHChgJ9y2_qA@mail.gmail.com \
    --to=kaushal.modi@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.