• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

equivalent of PB Shrink$ function

Started by Chris Chancellor, May 12, 2018, 05:34:49 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Chris Chancellor

Hello Charles

what is the equivalent of PB Shrink$ function ?

This Shrink$ function is useful when we need to remove the interleave blank space in a string

      NewString$ = SHRINK$(OldString$)


Remarks
The purpose of this function is to create a string with consecutive data items (words) separated by a consistent single
character. This makes it very straightforward to parse the results as needed.

In the first form, all leading spaces and trailing spaces are removed entirely. All occurrences of two or
more spaces are changed to a single space. Therefore, the new string returned consists of zero or more
words, each separated by a single space character.


for example    Newst = " yy -    gg - KK  -56 "
                        NewSt = SHRINK$(NewSt)
                  '  results in
                        Newst = "yy-gg-KK-56"

see http://www.manmrk.net/tutorials/basic/PowerBASIC/pbcc/shrink%24_function.htm






Charles Pegge

Chris,

Here is the shrink function for ascii strings. What I will do is create a PowerBasic section in the \ProjectsC folder, for compatibility functions like this one.


'2018-05-12 T 06:15:57
'PB shrink
'itr wide strings
uses console

function shrink(string s,m=" ") as string
=========================================
indexbase 1
int le=len s
int lm=len m
string t=nuls le
byte bs at strptr s
byte bm at strptr m
byte bt at strptr t
byte b
int c,i,j,k
c=1 'first char shrinkable
while i<le
  i++
  b=bs
  for k=1 to lm 'check with shrink chars
    if bm[k]=bs then 'matched
      if i=le then
        if c>=1 then
          if j then j-- 'remove prev shrink char
        end if
        exit while 'last char shrinkable
      end if
      c++ 'count
      if c>1 then
        @bs++ : continue while 'skip this char
      end if
      b=bm 'to assign first m character
    end if 'matched shrink char
  next 'shrink char check
  if b<>bm then
    c=0 'reset counter
  end if
  bt=b
  j++
  @bs++
  @bt++ 
loop
return left t,j
end function
'
'TESTS
======
print shrink ("12  34 , ,  56") cr
print shrink ("12  34 , ,  56"," ") cr
print shrink ("12  34 , ,  56"," ,") cr
print shrink ("12  34 , ,  56",", ") cr
print shrink (",,12  34 , ,  56, , , ",", ") cr
print shrink (",",",,") cr
print shrink (",",",") cr
wait

Chris Chancellor

Thanxx a lot Charles

You are a maestro !