November 28, 2020, 01:37:39 PM

Author Topic: [bmx] Word Wrap - Version #1 by Shortwind [ 1+ years ago ]  (Read 919 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Word Wrap - Version #1 by Shortwind [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Word Wrap - Version #1
Author : Shortwind
Posted : 1+ years ago

Description : Usefull word wrap routine for restricting printing of strings to a specific width.  It correctly handles 99% of all these situations, including correct printing when a section of text with out spaces is longer than the specified width.  This routine can easily be adapted to  other situations.

Code :
Code: BlitzMax
  1. SuperStrict
  2.  
  3. Local a:String="This string is very long and we (x+y=34*sqr(z)/cos(r)*pi+(x*y)/87) will test various length texts."+..
  4.         "  To test these functions for proper goodness is the stuff of dreams.  And "+..
  5.         "the dreams are (x+y=34*sqr(z)/cos(r)*pi+(x*y)/87) here and not there.  But what if the dreams and here"+..
  6.         " and not there?  Well, if the first and second part are not in the third part then the third part is "+..
  7.         "not the circle of the square.  But sometimes things are not as they seem.  Even though the seem is there,"+..
  8.         "it may not be there when we look into the seemingly endlessness of space and time...  Oh, but there is"+..
  9.         "no time like the present even though there is no present in the time of the timeness that is time."
  10.  
  11. Local boxwidth:Int=20
  12.  
  13. DoMyWrap(a, boxwidth)
  14. End
  15.  
  16. '------------------Start of Function ----------------------------
  17. Function DoMyWrap(a:String, width:Int)
  18.         Local s:Int, e:Int
  19.        
  20.         s=1
  21.         e=width
  22.        
  23.         While s<Len(a)
  24.                 If Len(a)<e Then
  25.                         Print a
  26.                         Exit
  27.                 ElseIf s+e>=Len(a) Then
  28.                         Print Mid(a,s)
  29.                         Exit
  30.                 End If
  31.                
  32.                 While Mid(a,s+e,1)<>" " And e>0
  33.                         e=e-1
  34.                 Wend
  35.                
  36.                 If e=0 Then
  37.                         Print Mid(a,s,width)
  38.                         s=s+width
  39.                         e=width
  40.                 Else
  41.                         Print Mid(a,s,e)
  42.                         s=s+e+1
  43.                         e=width
  44.                 End If
  45.         Wend
  46.  
  47. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal