Author Topic: Variable-length strings in TYPEs  (Read 10660 times)

0 Members and 1 Guest are viewing this topic.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Variable-length strings in TYPEs
« Reply #15 on: October 23, 2018, 10:36:58 pm »
And that's if you set a 2GB (or 4GB if you use an UNSIGNED LONG) limit to store that string length).  Since some folks (like soniaa in the Dim A thread) need to access larger sizes than that, an INTEGER64 would probably need to be used to stop any issues and work for future expansion, so you'd probably have to add 8 bytes per string...


Even the longest book ever written comes in at under 10 million(estimated) ASCII characters. And you have to consider memory size(the DIM A thread) in there as well. And data structure, so a limit of UNSIGNED LONG(cause you can't have a negative string anyway) is more than enough in any practical application. and even at that you could only have 4 or 5 arrayed of that type on most typical computers. While I agree that special occasions may call for a much larger value, most users of QB64 aren't going to have machines that could handle it. And large corporations like Apple or Google or the like that would have the means to have machines with terabyte and petabyte ram values will probably have efficient data structures or custom software anyway, you know this. So just for simplicity my vote is on keeping it small, either a string terminator(0A0D like most text editors maybe) or pre-string LOS(Length Of String) integer.

Don't get me wrong I see where you are coming from I just don't see the practical simplistic side of it.
Granted after becoming radioactive I only have a half-life!

Offline codeguy

  • Forum Regular
  • Posts: 174
    • View Profile
Re: Variable-length strings in TYPEs
« Reply #16 on: October 24, 2018, 12:38:58 pm »
Here's an idea.
A fixed delimiter of any size (in this case a*), namely sep$ for arbitrary lengths.
Code: QB64: [Select]
  1. TYPE Identity
  2.     FirstName AS STRING * 256
  3.     Lastname AS STRING * 32
  4.     NamesOfPets AS STRING '* does not work without String * nnnn
  5. DIM qx AS Identity
  6. '                  1      2         3   4              5    6             7        8              9                                       X
  7. Del$ = "*a"
  8. qx.FirstName = del$+"thea*bomina*bleSnowMa*nWa*sHereVistingSa*squa*tchThisIsARea*llyLongNa*meAndNobodyWa*ntsToWriteThisOutCompletelyAMillionSqua*redTimes"
  9. NthOneToGet& = 7 '7 = llyLongN using del$ as delimiter
  10.  
  11. PRINT GrabThatNth$(qx.FirstName, NthOneToGet&)
  12. FUNCTION GrabThatNth$ (m$, n&)
  13.     For I = 1 to Len(m$)
  14.            If asc(m$,I)=0 then
  15.                Exit for
  16.            End if
  17.     Next
  18.     Sep$=left$(m$,I)
  19.     p& = Len(sep$)+1
  20.     c& = 0
  21.     DO
  22.         m& = INSTR(p&, m$, sep$)
  23.         IF m& < 1 THEN
  24.             GrabThatNth$ = MID$(m$, p&)
  25.             EXIT DO
  26.         ELSEIF c& < n& THEN
  27.             p& = m& + sepLen&
  28.             c& = c& + 1
  29.         ELSE
  30.             GrabThatNth$ = MID$(m$, p&, m& - p&)
  31.             EXIT DO
  32.         END IF
  33.     LOOP
  34.  
« Last Edit: October 24, 2018, 02:49:42 pm by codeguy »

FellippeHeitor

  • Guest
Re: Variable-length strings in TYPEs
« Reply #17 on: October 24, 2018, 01:14:38 pm »
Say what??

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Variable-length strings in TYPEs
« Reply #18 on: October 24, 2018, 01:23:17 pm »
Say what??

No idea.  I didn't comprehend what Codeguy was referring to either. 
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline codeguy

  • Forum Regular
  • Posts: 174
    • View Profile
Re: Variable-length strings in TYPEs
« Reply #19 on: October 24, 2018, 02:11:04 pm »
The approach I use is for the string using fixed-length delimiters to find the Nth substring, no matter how far away the next delimeter is. The delimiter could be any length so as not to conflict with Individual strings already contained. Maybe a random string prepended with character 0 appended to that eg:
RsYv109!+chr$(0)+some stuff betweendelimeter+RsYv109!+chr$(0)+thenextString+RsYv109!+chr$(0)...
You get the idea. The lengths of substrings can then be limited only by memory. Hope that helps.
« Last Edit: October 24, 2018, 02:56:47 pm by codeguy »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Variable-length strings in TYPEs
« Reply #20 on: October 24, 2018, 06:16:01 pm »
Quote
Using a special mark like end of string is a procedural solution. The right issue noted by Steve can be minimized using a big mark as EOF (a sequence of 4-8 byte),so we change the case of premature end of string rare but not impossible.

so IMHO Codeguy suggests to use this mark EndOfString to be able to distinguish among the single string (substring), it is a way if we have a UDT made only with strings of variable length .... like
Code: QB64: [Select]
  1.  FirstString AS STRING
  2.  SecondString AS STRING
  3.  ThirdString AS STRING
  4.  
  5. ' using a pre_fixed_length delimiter QB64 can package all this Strings in a one SuperString to PUT# and/or GET#
  6. ' internally QB64 makes these operations:
  7.  
  8. DIM SuperString AS STRING ,ContStringAS INTEGER
  9. ContString = 3
  10. SuperString = FirstString + Delimiter$+ SecondString + Delimiter$+ ThirdString+ Delimiter$
  11. PUT/GET#1, SuperString
  12.  
  13. FOR cont% = 1 TO ContString
  14. IF  INSTR(Delimt%+1,SuperString, Delimiter$) THEN
  15. ' if there is a Delimiter$ at its left there is a string
  16.  IF cont%= 1 THEN FirstString = ExtractString SuperString, Delimiter$
  17.  IF cont%= 2 THEN SecondString = ExtractString SuperString, Delimiter$
  18.  IF cont%= 3 THEN ThirdString = ExtractString SuperString, Delimiter$
  19.  
  20. FUNCTION ExtractString (SupStr$, Delim$)
  21. Delimt%= INSTR(Delimt%+1,SupStr$, Delim$) ' it finds the end of a string
  22.  ExtractString =MID$ (SupStr$,1,Delimt%)  'it returns the string at left of SupStr$
  23. SupStr$ = RIGHT$(SupStr$,LEN(SupStr$)-( Delimt% + LEN (Delim$))) ' it returns the rest of SupStr$ without the last extracted string
  24.  

IMHO this case is like the array of string
Quote

TYPE
 FirstString AS STRING
 SecondString AS STRING
 ThirdString AS STRING
END TYPE

is equal to write

Quote
CONST FirstString = 1, SecondString =2, ThirdString = 3
DIM ArrayString (FirstString TO ThirdString) AS STRING

IMHO QB64 can manage PUT#/GET# with ArrayString.
So it is possible that QB64 internally can translate an UDT of strings variable to an array of Strings

--------
On the other hand, but how do Qbasic/QB4.5/QB7.1  create/manage strings in RAM?
Programming isn't difficult, only it's  consuming time and coffee

Offline codeguy

  • Forum Regular
  • Posts: 174
    • View Profile
Re: Variable-length strings in TYPEs
« Reply #21 on: October 24, 2018, 09:59:07 pm »
Something along the lines of what's pictured. I will upload a doable and faster version with substring splits loaded to an array for FAR easier and faster string lookup. It may have to involve HashTables and multidimensional arrays to be more usable for very large strings and with many substrings. It would greatly reduce the horsepower a CPU needs to extract the 999,368th substring for example.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Variable-length strings in TYPEs
« Reply #22 on: October 24, 2018, 10:38:13 pm »
Either that or linked lists using _get and _put yet to be created that use linked lists or something else... ;)

Offline pinology

  • Newbie
  • Posts: 17
    • View Profile
Re: Variable-length strings in TYPEs
« Reply #23 on: October 26, 2018, 08:33:07 am »
kind of along the same lines I was wondering if you could use an array in a type variable something like
type a
  b(20) as integer
  c as string * 5
end type

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Variable-length strings in TYPEs
« Reply #24 on: October 26, 2018, 09:06:36 am »
kind of along the same lines I was wondering if you could use an array in a type variable something like
type a
  b(20) as integer
  c as string * 5
end type
That's on the cards as the next thing to work on

Offline pinology

  • Newbie
  • Posts: 17
    • View Profile
Re: Variable-length strings in TYPEs
« Reply #25 on: October 26, 2018, 09:15:04 pm »
cool luke, that would come in real handy for putting data into random files and being able to access some of your variables numerically

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: Variable-length strings in TYPEs
« Reply #26 on: October 27, 2018, 03:02:59 am »
kind of along the same lines I was wondering if you could use an array in a type variable something like
type a
  b(20) as integer
  c as string * 5
end type
That's on the cards as the next thing to work on

Oh man, that would make writing this updated sprite library so much easier, LOL.
In order to understand recursion, one must first understand recursion.

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Variable-length strings in TYPEs
« Reply #27 on: October 27, 2018, 09:22:04 am »
Making GET and PUT work in any fashion turned out to be much harder and more work than I expected, since I need to find a way to embed UDT structural info into the final executable. Until I do that, I have published the changes allowing for variable length strings in UDT's to the development build of QB64. I would appreciate any attempts to find bugs with it (bugs are likely to be crashes, garbage data in variables, compiler errors when there shouldn't be).

FellippeHeitor

  • Guest
Re: Variable-length strings in TYPEs
« Reply #28 on: October 27, 2018, 12:15:56 pm »
Good news!

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Variable-length strings in TYPEs
« Reply #29 on: October 27, 2018, 10:59:06 pm »
Hey, Luke, LEN() always returns 4 regardless how many characters are in the string.
Granted after becoming radioactive I only have a half-life!