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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fac41,9a0ff0bffdf63657 X-Google-Attributes: gidfac41,public X-Google-Thread: 1108a1,9a0ff0bffdf63657 X-Google-Attributes: gid1108a1,public X-Google-Thread: 103376,4b06f8f15f01a568 X-Google-Attributes: gid103376,public X-Google-Thread: f43e6,9a0ff0bffdf63657 X-Google-Attributes: gidf43e6,public From: eachus@spectre.mitre.org (Robert I. Eachus) Subject: Re: Software landmines (loops) Date: 1998/09/18 Message-ID: #1/1 X-Deja-AN: 392264776 References: <35f51e53.48044143@ <35EEF597.A1119EF4@draper.com> <35F5B529.6DC6A59A@draper.com> Organization: The Mitre Corp., Bedford, MA. Newsgroups: comp.lang.eiffel,comp.object,comp.software-eng,comp.lang.ada Date: 1998-09-18T00:00:00+00:00 List-Id: In article Matthew Heaney writes: > I have a string that was padded on the right with blanks. What I need > to do is strip off the blanks, and return the index of the last > non-blank character. > Let's analyze this problem first using Dijkstra's predicate transformer, > and then using Ada... Not to disagree with Matt, because he is trying to give an example of how to reason about a problem, but there is a better approach: Ada.Strings.Fixed.Trim(S,Right)'Last Of course, you probably want to use the value returned from Trim instead of just the index of the last non-blank character. (Technically, if you want to insure that the value returned for a null string is zero, you need to do: function Last_Non_Blank_Index(S: String) return Natural is Temp : constant Natural := Ada.Strings.Fixed.Trim(S,Right)'Last; begin if Temp = S'First then return 0; else return Temp; end if; end Last_Non_Blank_Index; but the convention of returning 0 in this case was not part of the original problem statement: > I have a string that was padded on the right with blanks. What I > need to do is strip off the blanks, and return the index of the > last non-blank character. Why bring this up? Because when doing ANY kind of engineering, the first approach to try should be to ask, "Is there something in the standard catalog that is available off-the-shelf?" It is so easy and enjoyable to solve these problems in Ada, that we often lose sight of how much grief using the library function will save later. For example: If you need to truncate from both ends? Change "Right" to "Both". If you need to truncate spaces and horizontal tabs from both ends, and commas and periods from the right: with Ada.Strings.Maps, Ada.Characters.Latin_1; use Ada.Strings.Maps, Ada.Characters.Latin_1; ... Ada.Strings.Fixed.Trim(S, To_Set(Space & HT), To_Set(Space & HT & Comma & Full_Stop); -- Although I would probably declare the Character_Sets as -- constants in some package. -- Robert I. Eachus with Standard_Disclaimer; use Standard_Disclaimer; function Message (Text: in Clever_Ideas) return Better_Ideas is...