views:

363

answers:

3

Hi, all. I was wondering if Emacs lisp had a built-in function for checking if a string is made entirely out of capitalized characters. Here is what I'm using right now:

(setq capital-letters (string-to-list "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))

(defun chars-are-capitalized (list-of-characters)
  "Returns true if every character in a list of characters is a capital         
letter. As a special case, the empty list returns true."
  (cond
   ((equal list-of-characters nil) t)
   ((not (member (car list-of-characters) capital-letters)) nil)
   (t (chars-are-capitalized (cdr list-of-characters)))))

(defun string-is-capitalized (string)
  "Returns true if every character in a string is a capital letter. The         
empty string returns true."
  (chars-are-capitalized (string-to-list string)))

It works ok (although it relies on the assumption that I'll only be using ASCII characters), but I was wondering if I was missing some obvious function that I should know about.

+4  A: 

I don't know of a built-in function that does what you want, but this does:

(defun string-all-caps-p (string)
  "Return non-nil iff STRING is all capital letters."
  (save-match-data
    (let ((case-fold-search nil))
      (string-match "\\`[A-Z]+\\'" string))))

Edit: Changed to use ` and ' as per Eli Barzilay's feedback.

This one lets there be non A-Z chars (not what you asked for, but perhaps interesting):

(defun string-has-no-lowercase (string)
  "Return true iff STRING has no lowercase"
  (equal (upcase string) string))
Trey Jackson
That first one makes sense; I should have thought of a regex. I'd already thought of checking (equal string (upcase string)), but I do need it to return false if the string has any non-alphabetical letters.
Masterofpsi
For the first one, you probably want to wrap that in a `save-match-data` call, so any existing regex match data doesn't get overwritten.
haxney
@dhax True, thanks.
Trey Jackson
A: 

just a wild guess, but what if you make a copy of the string, upcase it (i dont really know much about lisp but a quick google search told there is a "upcase" function, and then check if the two strings are the same? If the are, then the original one must be in all upperscase :P

Francisco Noriega
+2  A: 

In reference to other answers:

  1. Using upcase is not a good idea: it will allocate a new string, it will not find if the string has non-alphabetic characters (it seems that you want to forbid that), and it works on integers too (which Emacs uses for characters).

  2. Using string-match is better -- it fixes all of these issues. As Trey shows, you need to do that when case-fold-search is nil otherwise Emacs might treat it as a case-insensitive search. But string-match-p is even better since it avoids changing the match data. (Emacs keeps that data around after any match, and if you use string-match then you'll overwrite it, which might break code that uses your function.)

  3. Another issue is the regexp itself. Using "^...$" means that Emacs will look for some line with a matching content -- and if your string has newline characters, this might make it return a bogus result. You need to use backslash-unquote and backslash-quote which match only the beginning and end of the string.

So a correct version is:

(defun string-is-capitalized (str)
  (let ((case-fold-search nil))
    (string-match-p "\\`[A-Z]*\\'" str)))

(BTW, the usual convention in Emacs Lisp is to use a -p for predicates, as in string-capitalized-p.)

Eli Barzilay
Good point about using ` and ' in the regexp.
Trey Jackson