From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,4fbd260da735f6f4 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!news.glorb.com!news2.arglkargh.de!noris.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Copying string slices before calling subroutines? Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <0hj5339mjmond132qhbn2o01unurs61lbj@4ax.com> <1178091967.392381.282510@o5g2000hsb.googlegroups.com> <5dv3wh6scrh1.2986pbvdw8y2$.dlg@40tude.net> <4oppvmc6anir.g0mz9mrahmdo.dlg@40tude.net> <4akrg4-mot.ln1@newserver.thecreems.com> Date: Sat, 5 May 2007 09:41:57 +0200 Message-ID: <1jz12blpydzq0.a66zm8kaza1d.dlg@40tude.net> NNTP-Posting-Date: 05 May 2007 09:41:22 CEST NNTP-Posting-Host: c58ebf70.newsspool1.arcor-online.net X-Trace: DXC=Cf\BH3YRQeFdg=efPDRDNcfSJ;bb[UIRnRBaCd On Fri, 04 May 2007 23:27:57 +0100, Simon Wright wrote: > My reworking begins > > function Index > (Source : String; > Pattern : String; > Going : Ada.Strings.Direction := Ada.Strings.Forward; > Mapping : Ada.Strings.Maps.Character_Mapping > := Ada.Strings.Maps.Identity) return Natural > is > Cur_Index : Natural; > Potential_Match : Boolean; > use Ada.Strings; > use Ada.Strings.Maps; > begin > if Pattern = "" then > raise Pattern_Error; > end if; > > -- Forwards case > > if Going = Forward then > for J in 1 .. Source'Length - Pattern'Length + 1 loop > Cur_Index := Source'First + J - 1; > Potential_Match := True; > for K in Pattern'Range loop > if Pattern (K) /= > Value (Mapping, Source (Cur_Index + K - 1)) then > Potential_Match := False; > exit; > end if; > end loop; > if Potential_Match then > return Cur_Index; > end if; > end loop; > > which calls Ada.Strings.Maps.Value rather more often than I suppose it > could. You can save remapping of Source, just there is no need to do it in advance and allocate a full copy of Source. Instead of that, you make a ring buffer of mod Pattern'Length where you store Value (Mapping, Source (i)). Once you advance the main string index you "rotate" the buffer. Then Pattern'Length = 1 should be a special case, as well as identity mapping. Though, I don't know how efficient the latter could be. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de