function to calculate string length...

Dick Kriesel dick.kriesel at mail.com
Sun Dec 26 16:21:31 EST 2004


Chipp --

I'm not sure these opportunities to optimize are either obvious or
significant for your application, but I'd welcome your assessment.

-- for each excess character, execute a put into the field rather than a
delete, because the delete involves both reading and writing the field

-- for each line that has an excess character, append the shortened line to
tList without reading the field

-- execute fewer lines of code in each repeat loop

-- refer to the field by number rather than by name

-- obviate field "formatCheck"

-- delete only enough characters to make the ellipsis fit

-- execute no repeat loop if the field is too narrow for the ellipsis

(Watch out for the email line wrapping.)

on formatFld pFldName, pTxt
  lock screen
  put the number of field pFldName into tFieldNumber
  put the width of field tFieldNumber into tWidth
  put "..." into field tFieldNumber
  if the formattedWidth of field tFieldNumber > tWidth then put pTxt into
field tFieldNumber
  else
    repeat for each line tLine in pTxt
      put tLine into field tFieldNumber
      if the formattedWidth of field tFieldNumber > tWidth then
        put (char 1 to -2 of tLine) & "..." into tLine
        repeat forever
          put tLine into field tFieldNumber
          if the formattedWidth of field tFieldNumber > tWidth then delete
char -4 of tLine
          else exit repeat
        end repeat
      end if
      put tLine & cr after tList
    end repeat
    put char 1 to -2 of tList into field tFieldNumber
  end if
  unlock screen
end formatFld

Although it's beyond just optimizing the code, you could set the textStyle
of the line that's too long to "condensed" before you start trimming
characters.

-- Dick


On 12/26/04 4:21 AM, "Chipp Walters" <chipp at chipp.com> wrote:

> Well, I cobbled this together from some of the archives...
> Anyone see an obvious place to optimize?
> 
> on formatFld pFldName, pTxt
>    lock screen
>    put the width of fld pFldName into tW
>    set the width of fld "formatCheck" to tW
>    repeat for each line L in pTxt
>      put L into fld "formatCheck"
>      put false into tIsTooLong
>      repeat while the formattedwidth of fld "formatCheck" > tW
>        delete last char of fld "formatCheck"
>        put true into tIsTooLong
>      end repeat
>      if tIsTooLong is true then
>        put fld "formatCheck" after tList
>        delete char -5 to -1 of tList
>        put "..." & cr after tList
>      else
>        put L & cr after tList
>      end if
>    end repeat
>    delete last char of tList
>    put tList into fld pFldName
>    unlock screen
> end formatFld
> 
> 
> Chipp Walters wrote:
>> Looking for some help.
>> 
>> I have a list field and want to add an elipsis (...) at the end of lines
>> which are truncated and won't fit in the field. I was wondering if
>> anyone out there may have a function to do this quickly?
> 




More information about the use-livecode mailing list