Removing Repeated Occurrences of a Target Character From a String

The Problem

While I was going through my RSS feed the other day, I came across this Programming Praxis problem and thought right away that the solution called for a state machine. It’s one of those problems that seems easy enough but if you (or, at least, I) try to write it straight out without a state machine you end up drowning in little details and inevitably get something wrong.

Later, I thought that it would serve as an excellent example of Christopher Wellons’ buffer passing style that I wrote about previously. I coded it up from memory so of course I got the problem wrong. Not just wrong but backwards. It doesn’t really matter for our purposes so the problem we’re solving is to remove any repeated instance of the target character. Thus if X is the target character, then XabXXcdXeXXXfXXabcdXefX.

We want to write a function, remove-repeated that will take the target character and a string and remove all repeated sequences of the target character. You can see right away why it’s a perfect example of the problem Wellons was discussing: we need to build an output string piece by piece so the buffer passing style is just what we need.

The State Machine

This is a pretty simple problem so a simple state machine with three states is all we need.

remove-sm.png

The state diagram doesn’t show any actions—see the code for that—it just lists the states and their transitions. In the diagram, X stands for the target character and ANY is any other character.

The Code

Here’s the state machine implemented in Emacs Lisp. If there were a lot of states, the case-style implementation wouldn’t be very efficient but it’s fine for our three states.

 1: (defun remove-repeated (ch-to-remove string)
 2:   "Remove any sequence of two or more occurrences of CH-TO-REMOVE
 3: from STRING."
 4:   (let ((state :start))
 5:     (setq case-fold-search nil)
 6:     (dolist (current-ch (string-to-list string))
 7:       (case state
 8:         (:start (if (char-equal current-ch ch-to-remove)
 9:                     (setq state :check-dupe)
10:                   (insert current-ch)))
11:         (:check-dupe (if (char-equal current-ch ch-to-remove)
12:                          (setq state :remove-dupe)
13:                        (insert ch-to-remove)
14:                        (insert current-ch)
15:                        (setq state :start)))
16:         (:remove-dupe (unless (char-equal current-ch ch-to-remove)
17:                         (insert current-ch)
18:                         (setq state :start)))))
19:     (when (eql state :check-dupe)
20:       (insert ch-to-remove))))

The when in line 19 takes care of the case where there is a single target character at the end of the string.

When we run the code with

(with-temp-buffer
  (remove-repeated ?X "XabXXcdXeXXXfX")
  (buffer-string))

we get XabcdXefX as expected. Notice how remove-repeated doesn’t bother building a string. It just puts the required characters in the current buffer and returns. The caller coerces it into a string and the temporary buffer is killed when control flows out of the with-temp-buffer form.

To illustrate the flexibility of this approach, suppose we want to return the length of the resulting string. We could, of course, just change the last line of the call to

(length (buffer-string))

but a more efficient solution is to simply replace the last line with

(buffer-size)

and not bother forming the actual string at all.

Posted in Programming | Tagged , | 2 Comments

Pictures of NSA Corrupting New Networking Equipment

We knew about the NSA’s intercepting network equipment and installing backdoor hardware before shipping it on to its intended destination. The idea, of course, was to enable easy access to networks that would otherwise hard to exploit. The NSA describes this program as one of their most productive TAO1 operations.

It’s one thing to know this but it’s nevertheless shocking to see actual pictures of them installing the hardware. The pictures are one of the most recent revelations in the on-going publication of the Snowden documents.

If you are a non-American buyer of networking equipment would you purchase from an American company? What if you are an American buyer? It’s not clear whether the vendors are complicit in this program or not but either way they should be doing everything in their power to disassociate themselves from it. Otherwise, customers will be running for the exits. Really, it’s hard to see why that’s not already happening.

If these vendors were complicit, then they’re getting what they deserve. If they weren’t, then they’ve got a real beef with the US government, which they should pursue in the courts.

Footnotes:

1

Tailored Access Operations.

Posted in General | Tagged | Leave a comment

The PhD Movie

If you’ve been a graduate student since the late 1990’s, you’re probably familiar with PhD Comics, a comic drawn by Jorge Cham that captures the absurdities of his graduate studies in a way that will resonate with any graduate student. If you have graduate student experience and haven’t seen PhD Comics, you should definitely add it to your daily reading list.

A couple of years ago, Cham recruited some graduate students to make a movie that captures some of the fun of the comic. It’s about an hour long and has been for sale since its release. Now Cham is raising funds for a second movie and as part of that effort you can view the first movie for free during June. Just go to this page to enjoy the fun. It’s amazing how well the actors capture the comic’s characters.

You probably have to have been a graduate student to appreciate the in-jokes and humor of the movie but if you have that experience, the movie is sure to bring back those days of joy and pain. I love this comic and read it every day and I really enjoyed the movie. Now’s your chance to see it for free.

If you like it or are a PhD Comics fan, you might want to kick in some funds for the new movie. Just follow the second link above for the kickstarter page.

Posted in General | Leave a comment

Magnar Continues His WebRebels Videos

The estimable Magnar Sveen has, to use his words, come out of hibernation and continued the posting of his WebRebels talk. This video is part 5 of the series.

It’s been a while so if you don’t remember, the official video of the talk had technical problems but Sveen has recreated it bit by bit. If you’ve never seen Sveen talk, it’s a real treat. He’s an excellent speaker and his mastery of Emacs is impressive.

He’s most known for his spectacular Emacs Rocks! videos. If you’re an Emacs user and haven’t watched them, stop right now and have a look. His original talk at the Oslo WebRebels is astounding and you should definitely watch it too. You can get links to the other videos for the current WebRebels talk at this page.

If you like these videos and want to learn a bit more about Sveen, Sacha Chua interviewed him in one of her Emacs Chats.

Posted in General | Tagged | Leave a comment

Mutable String and Emacs Buffer Passing Style

Speaking of Christopher Wellons, he’s got a very interesting post over at null program on mutable strings and Emacs buffer passing style. Wellons starts by pointing out that strings in Emacs Lisp, like many other languages, have a fixed size and any operation that changes the size requires that a new string be allocated and current content copied into it. That can be a problem when you’re building up a string incrementally. Next, he observes that Elisp has a natural character-oriented data structure: buffers. Why not use a buffer to build up the string components and then turn the buffer into a string with buffer-string. Something along the lines of:

(with-temp-buffer
  ;; build up the string
  (buffer-string))

That’s a good tip all by itself but Wellons goes further.

Suppose you are writing a function to construct a string. A natural technique is to have the function build the string in a buffer and then pass the buffer back to the caller who can do whatever they need with it (perhaps never coercing it into an actual string). The problem is that using that method makes it easy to leak buffers, which are not garbage collected. Wellons has a nice example of how that can happen in his post.

To avoid this problem, Wellons advocates what he calls a buffer passing style. With this approach, a buffer is implicitly passed to the function, which fills it with the desired data. “Implicitly” because the caller arranges for the current buffer to be the buffer that the function uses. Then the caller can kill the buffer after using the data. The actual idiom is something like:

(with-temp-buffer
  (fill-buffer-with-desired-data)       ;string building function
  ;; process the data
  )

This is nice because the with-temp-buffer macro takes care of killing the buffer when you reach the end of the macro body.

Take a look at Wellons’ post. It’s very informative and gives some fleshed out, realistic examples rather than the skeletons I give here.

Posted in Programming | Tagged , | Leave a comment

Sacha Chats with Christopher Wellons

The invaluable Sacha Chua has posted another in her series of Emacs chats. This time it’s with Christopher Wellons. As regular readers know, I’m an admirer of Wellons and have written about him several times (1, 2, 3, 4, 5).

As usual, they talk about Wellon’s work flow, his Emacs configuration, and his projects. As you can see from my previous posts on Wellons, he’s the type of guy who is always working on little, interesting projects that often grow into something really useful. Watch the video to get an idea of what those projects are. Chua has a handy list of links to them on Github so that you can explore them after watching the video.

One of the things that I really liked was his with-package and with-package* macros. They make sure that your packages are loaded and that any package specific actions are taken at the proper time. I really like this because it provides a way of listing the packages you use in your init.el or .emacs file and then they get loaded automatically if they aren’t already on your machine. Right now, I do this manually for each of my machines and that’s just silly and error prone.

Wellons says that the macros are a little rough but I found them perfectly reasonable and the code is very nice as well. I’ll be integrating them into my own configuration directly.

The chat is just under 57 minutes so plan accordingly. It’s a great chat and one you won’t want to miss.

Posted in General | Tagged | 2 Comments

Grant Rettke’s Take on Narrowing to Multiple Regions

Grant Rettke has his own take on a function to enable narrowing to multiple regions of a buffer. I learned about the idea from comments to a post on another matter and wrote about it here. Rettke takes the example functions that I wrote about and adjusts them to suit his own needs.

I really like that this idea is getting more exposure. As Rettke says, all the ideas are there in the Emacs manual but it takes guys like Zane Ashby to see how they can be used to solve a particular problem. I also like Rettke’s characterization of the problem as a sort of “semi-literate programming.” It seemed strange when I first read it but that’s exactly what’s going on: you can treat different parts of the buffer in different ways to create a whole.

If you want another take on the functions that Zane and grayswx wrote, head on over to Rettke’s post and take a look.

Update: Retkke → Rettke. Sorry Grant.

Posted in Programming | Tagged | Leave a comment

Turning On Undo in a Temporary Buffer

Today I learned something new from a short post on Xah Lee’s blog. The idea is that you’ve written a command that generates output in a temporary buffer and you want to enable the undo function for the buffer so that the user can edit the buffer with undo capabilities.

You can’t link to individual posts on Lee’s blog but you can search for the date of the post, which is 2014-05-30. Here’s the code from that post in case you have trouble finding the post:

(with-output-to-temp-buffer outputBuffer
  ;; 
  (switch-to-buffer outputBuffer)
  (setq buffer-undo-list nil )          ; enable undo
  )

As you can see, it’s merely a matter of providing an empty buffer-undo-list for the buffer. This isn’t the sort of thing you’re going to need very often and it would probably be pretty hard to find it in the documentation1. So this post is mainly a note to myself although I hope it might help others who didn’t know this fact.

Footnotes:

1

Well, maybe not. As Lee points out, you can find the information by looking at the undo node in the Elisp manual.

Posted in Programming | Tagged | 3 Comments

Second Year of Daily Posts

Today is the second anniversary of daily posts on Irreal. For the past two years, Irreal has published at least one post a day.

Last year I remarked that it was a lot harder than one would think. Nothing that’s happened this year has given me any reason to change my mind on that. Some days I had absolutely no reserve posts and had to come up with something or break the chain. Fortunately, my muse always came through—even if at the last moment—and I was able to keep going.

I’m going to keep on keeping on so my hope is that on June 1st next year I’ll be able to celebrate three years of daily posts. So. Onward and upward.

Posted in Blogging | 1 Comment

Twenty Questions for Donald Knuth

Over at Informit they’ve got a great Q&A with Donald Knuth. Twenty luminaries in computer science ask Knuth questions about himself, his work, and computer science. The questions are much more interesting than the usual session of this sort so it’s definitely worth taking a look.

Among the questioners are Jon Bentley, Radia Perlman, Robert Sedgewick, Silvio Levy, Al Aho, Guy Steele, Robert Tarjan, and Andrew Binstock, just to mention a few who are familiar to me. It’s no wonder the questions are of such high caliber.

If you read the entire article, you’ll learn lots of interesting things. For me, the most astounding thing was to learn (or more precisely, be reminded) that Knuth has been working on The Art of Computer Programming for more than 50 years. I’m pretty sure that that’s longer than most Irreal readers have been alive. As I say, astounding.

Posted in General | Leave a comment