help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A variant of match-end, but after replacement?


From: Pascal J. Bourguignon
Subject: Re: A variant of match-end, but after replacement?
Date: Mon, 20 Jul 2015 02:33:13 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Marcin Borkowski <mbork@mbork.pl> writes:
>> Not very much:
>>
>>     (let ((end        (match-end 1))
>>           (new-string (replace-match new-text t t string 1)))
>>       (do-something-from new-string
>>                          (+ end (- (length new-string) (length string)))))
>
> I give up.  It seems that a PhD in math is not enough to deal with
> string-length arithmetic;-).  Shame on me!  (OTOH, mails from you often
> teach me that when I have the impression that I'm smart, it's usually
> only an impression.)

Sorry, I was about to write out the equation, but if it's necessary,
I'll do.



   (length new-string)
   --------------------------------------------------------
   (length prefix)  (length new)           (length suffix)
   ---------------- ---------------------- ----------------
   pppppppppppppppp nnnnnnnnnnnnnnnnnnnnnn ssssssssssssssss
                                          ^
                                          |
                                       new-end

   (length old-string)
   --------------------------------------------------
   (length prefix)  (length old)     (length suffix)
   ---------------- ---------------- ----------------
   pppppppppppppppp oooooooooooooooo ssssssssssssssss
                                    ^
                                    |
                               (match-end)


So, we have:

    (= (length suffix) (- (length new-string) new-end))
    (= (length suffix) (- (length old-string) (match-end)))

Or, in math:

       s = ns-ne
       s = os-me
    
Therefore:

        0 = (ns-ne) - (os-me)
  <=>   0 = ns - ne - os + me
  <=>  ne = me + (ns - os)
  <=> (= new-end (+ (match-end) (- (length new-string) (length old-string))))
      
Hence:
           
     (let ((end        (match-end 1))
           (new-string (replace-match new-text t t old-string 1)))
       (do-something-from new-string
                          (+ end (- (length new-string) (length old-string)))))


-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


reply via email to

[Prev in Thread] Current Thread [Next in Thread]