Constant request: #LOREM$

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
bosker
Enthusiast
Enthusiast
Posts: 105
Joined: Fri Jan 08, 2010 11:04 pm
Location: Hampshire, UK

Re: Constant request: #LOREM$

Post by bosker »

Why not create #LOREM$ in a resident. Isn't this the kind of thing they are there for?
I have a bunch of macros and "stuff" that I want (need?) in residents. Always available - works well.
User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

Re: Constant request: #LOREM$

Post by Tenaja »

bosker wrote:Why not create #LOREM$ in a resident. Isn't this the kind of thing they are there for?
I have a bunch of macros and "stuff" that I want (need?) in residents. Always available - works well.
That just adds frustration in these situations:
PB Fanatic wrote:...I'm talking about when sharing code in these forums, too...
Gadget
User
User
Posts: 38
Joined: Tue Mar 11, 2014 8:11 pm
Location: UK

Re: Constant request: #LOREM$

Post by Gadget »

..... How about a single .pbi include file, posted as a sticky on the forum? Everybody could update that post so that we have a comprehensive file to support a range of code snippets......
+1
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Constant request: #LOREM$

Post by heartbone »

Imbedding test data into the compiler is IMO bloat.
An optional include file makes much more sense, as does bosker's suggestion.
I even like my suggestion to include a garbage generator better than including a #LOREM constant.
Enough typed.
Keep it BASIC.
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Constant request: #LOREM$

Post by Vera »

skywalk wrote:The same goes for simple images which can be found in the install folder, like #PB_Compiler_Home+"Examples\Sources\Data\CdPlayer.ico".
Well there are texts one could use, but you immediately have to regard the different OSs, unless your code isn't already determined.

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  file$ = "readme.txt"
CompilerElse
  file$ = "README"
CompilerEndIf

ReadFile(1, #PB_Compiler_Home + file$)
While Eof(1) = 0
  astring$ = ReadString(1)
  If l > 10 And l < 19
    string$ = string$ +Chr(32)+ astring$
    EndIf 
  l+1
Wend
CloseFile(1)
Debug string$
End
If one only wants to grab any snippet of any text one could simply copy what's currently in sight and paste it into the waiting quotes. If it's needed more often one could implement a kind of shortcut to

Code: Select all

Debug RemoveString(GetClipboardText(), Chr(10))
Or if it should be a copyright safe individual sized block of sense-free random text ?
... you can try my "#LOREM$ your own" :mrgreen:

Code: Select all

Procedure.s Lorem(length.i)

  For i = 1 To length
    If an = 0                        ; grant capital letter at startup & new sentence
      x = Random(25) + 65 : an = 1
    Else
      x = Random(24) + 97
    EndIf
    If ch <> 0                       ; prevent startup/sentence with space (resp. dot)
      a = Random(4)
      b = Random(3)
      If a = b Or ch > 9             ; create words by chance or if getting too long
        x  = 32 : ch = 0
      EndIf
    EndIf

    If z = x :x + 1                  ; prevent double-letters by moving x by one position
    ElseIf z = 33 : x = 46           ; create sentence if space (32+1) occured twice in a row
      lorem$ = lorem$ + Chr(x) + Chr(32) : ch = -1 : an = 0 
      lorem$ = ReplaceString(lorem$, " .", ".")     ; remove prior space & possible double-dots
      lorem$ = ReplaceString(lorem$, "..", ".")
    Else
      lorem$ = lorem$ + Chr(x)
    EndIf
    ch + 1                           ; count 'word-letters' 
    z = x                            ; set z to prevent double-letters
  Next

  lorem$ = RTrim(lorem$, " ")               ; clean up possible text-ending and apply s.th. defined
  lorem$ = RTrim(lorem$, ".")
  lorem$ = lorem$ + "." + Chr(10) + "~ q.e.d. ~"

  ProcedureReturn lorem$

EndProcedure


If OpenWindow(0, 0, 0, 310, 240, "#LOREM$ your own", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EditorGadget(0, 10, 10, 290, 190 ,#PB_Editor_WordWrap)
  TextGadget(3, 30, 210, 80, 22, "characters:")
  StringGadget(2, 100, 210, 50, 22, "400")
  ButtonGadget(1, 160, 210, 120, 22, "create lorem")

  CompilerIf #PB_Compiler_OS = #PB_OS_Linux
    gtk_text_view_set_wrap_mode_(GadgetID(0), #GTK_WRAP_WORD)
  CompilerEndIf 

  Repeat
    wwe = WaitWindowEvent()

    Select wwe
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            SetGadgetText(0, Lorem(Val(GetGadgetText(2))))
        EndSelect
    EndSelect

  Until wwe = #PB_Event_CloseWindow
EndIf
cheers ~ Vera
PB Fanatic
User
User
Posts: 49
Joined: Wed Dec 17, 2014 11:54 am

Re: Constant request: #LOREM$

Post by PB Fanatic »

The idea was to have a longish block of text always available for testing with examples, and implemented as a single constant that could be used. Rolling your own defeats this purpose. There are so many example codes in this forum that show how to do things (capitalize each word, pluck a word from inside a string, how to do word wrap) but each example includes a block of text made up with var$=var+whatever$ to make the example work. To me, a #LOREM$ constant with an always-available and reliable string of text, makes it easier to code such examples. That's all I'm asking. Just a request. Didn't mean to start a debate of why it's bad or why it sucks or why it's stupid.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Constant request: #LOREM$

Post by TI-994A »

PB Fanatic wrote:...Just a request. Didn't mean to start a debate of why it's bad or why it sucks or why it's stupid.
Hello, PB Fanatic. And welcome to the PureBasic forum. :D

In all honesty, the discussion only hovers on feasibility, and not functionality. Granted, there are merits to the request, and if the development team concurs, you'll have yourself a new feature.

In the time being, we're a very friendly bunch. Really! :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Constant request: #LOREM$

Post by Vera »

Take it lighthearted PB Fanatic :-)

Rarely a request gets so much creative attention and you surely have been heard.

~ have g 8) d days ahead ~
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: Constant request: #LOREM$

Post by Joakim Christiansen »

Tenaja wrote:
netmaestro wrote:
to get a large block of random text (for testing) makes absolute perfect sense
Does it make sense? Sure it does. Will it happen within 7 years? No, too many other things have priority. When the dev team has the time to add stuff like this to the language, we really will have arrived. Reclining on the beach, sipping margaritas and watching Apple Microsoft and Symantec straining to catch up. PureOS is on 78% of devices, Fred has just bought Hawaii and anyone with a PB license can vacation as Fantaisie's guest anytime. Ahh...
I cannot imagine the implementation taking any longer than a copy/paste, perhaps two or three additional keystrokes... if it takes longer than that, then he has set up constants with a convoluted implementation.
Agreed! Simple things like this could be added "immediately" without taking time from the bigger projects, so I don't agree with netmaestro on this. I say just add it, taking time to think too much about it takes more time than adding it. And no it doesn't cause much bloat to the compiler, it's a 64 bit world today and the compiler is still less than 1 MB (we can handle it)...
I like logic, hence I dislike humans but love computers.
PB Fanatic
User
User
Posts: 49
Joined: Wed Dec 17, 2014 11:54 am

Re: Constant request: #LOREM$

Post by PB Fanatic »

heartbone wrote:Imbedding test data into the compiler is IMO bloat.
Are you serious? A mere 445 characters, less than half a kilobyte, is considered bloat? If this feature ever makes it into the compiler, then you better start saving for a bigger hard drive right now, my friend. :lol:
PB Fanatic
User
User
Posts: 49
Joined: Wed Dec 17, 2014 11:54 am

Re: Constant request: #LOREM$

Post by PB Fanatic »

Vera wrote:... you can try my "#LOREM$ your own" :mrgreen:

Code: Select all

Procedure.s Lorem(length.i)

  For i = 1 To length
    If an = 0                        ; grant capital letter at startup & new sentence
      x = Random(25) + 65 : an = 1
    Else
      x = Random(24) + 97
    EndIf
    If ch <> 0                       ; prevent startup/sentence with space (resp. dot)
      a = Random(4)
      b = Random(3)
      If a = b Or ch > 9             ; create words by chance or if getting too long
        x  = 32 : ch = 0
      EndIf
    EndIf

    If z = x :x + 1                  ; prevent double-letters by moving x by one position
    ElseIf z = 33 : x = 46           ; create sentence if space (32+1) occured twice in a row
      lorem$ = lorem$ + Chr(x) + Chr(32) : ch = -1 : an = 0 
      lorem$ = ReplaceString(lorem$, " .", ".")     ; remove prior space & possible double-dots
      lorem$ = ReplaceString(lorem$, "..", ".")
    Else
      lorem$ = lorem$ + Chr(x)
    EndIf
    ch + 1                           ; count 'word-letters' 
    z = x                            ; set z to prevent double-letters
  Next

  lorem$ = RTrim(lorem$, " ")               ; clean up possible text-ending and apply s.th. defined
  lorem$ = RTrim(lorem$, ".")
  lorem$ = lorem$ + "." + Chr(10) + "~ q.e.d. ~"

  ProcedureReturn lorem$

EndProcedure


If OpenWindow(0, 0, 0, 310, 240, "#LOREM$ your own", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EditorGadget(0, 10, 10, 290, 190 ,#PB_Editor_WordWrap)
  TextGadget(3, 30, 210, 80, 22, "characters:")
  StringGadget(2, 100, 210, 50, 22, "400")
  ButtonGadget(1, 160, 210, 120, 22, "create lorem")

  CompilerIf #PB_Compiler_OS = #PB_OS_Linux
    gtk_text_view_set_wrap_mode_(GadgetID(0), #GTK_WRAP_WORD)
  CompilerEndIf 

  Repeat
    wwe = WaitWindowEvent()

    Select wwe
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            SetGadgetText(0, Lorem(Val(GetGadgetText(2))))
        EndSelect
    EndSelect

  Until wwe = #PB_Event_CloseWindow
EndIf
Now Vera, wouldn't you have much preferred to just have posted this:

Code: Select all

If OpenWindow(0, 0, 0, 310, 240, "#LOREM$ your own", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  EditorGadget(0, 10, 10, 290, 190 ,#PB_Editor_WordWrap)

  CompilerIf #PB_Compiler_OS = #PB_OS_Linux
    gtk_text_view_set_wrap_mode_(GadgetID(0), #GTK_WRAP_WORD)
  CompilerEndIf

  Repeat
    wwe = WaitWindowEvent()

    Select wwe
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            SetGadgetText(0, #LOREM$)
        EndSelect
    EndSelect

  Until wwe = #PB_Event_CloseWindow
EndIf
That's what my request is all about: reducing the need to build a big string for testing. Have one built-in, for all and sundry to use as they see fit. As Fred once said: "ha, dreams". :wink:
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Constant request: #LOREM$

Post by Vera »

PB Fanatic wrote:Now Vera, wouldn't you have much preferred to just have posted this: [...]
I'm not sure ... I don't think the outcome would be half as thrilling as mine :mrgreen:
That's what my request is all about: reducing the need to build a big string for testing.
Indeed, I got that.
... and I'm fine if it'll come one day :-)
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: Constant request: #LOREM$

Post by Rescator »

It is also possible to do this:

Code: Select all

Procedure.s RandomText1(length.l)
	Protected *mem, i.i, *text.Ascii, text$
	*mem = AllocateMemory(length, #PB_Memory_NoClear)
	RandomData(*mem, length)
	*text = *mem
	For i = 1 To length
		If *text\a > 122
			*text\a = *text\a & $3F
		EndIf
		If *text\a < 33
			*text\a = *text\a & $E0
		EndIf
		If *text\a = 0
			*text\a = 32
		EndIf
		*text + 1
	Next
	text$ = PeekS(*mem, length, #PB_Ascii)
	ProcedureReturn text$
EndProcedure

Debug RandomText1(64)

Code: Select all

Procedure.s RandomText2(length.l)
	Protected *mem, i.i, *text.Byte, text$

	Protected Dim chars.b(63), c.b
	For c = 48 To 57
		chars(i) = c
		i + 1
	Next
	For c = 63 To 90
		chars(i) = c
		i + 1
	Next
	For c = 97 To 122
		chars(i) = c
		i + 1
	Next

	*mem = AllocateMemory(length, #PB_Memory_NoClear)
	RandomData(*mem, length)
	*text = *mem
	For i = 1 To length
		If (*text\b & $8) And (i & $1)
			*text\b = 32
		Else
			*text\b = chars(*text\b & $3F)
		EndIF
		*text + 1
	Next
	text$ = PeekS(*mem, length, #PB_Ascii)
	ProcedureReturn text$
EndProcedure

Debug RandomText2(64)

Code: Select all

Procedure.s RandomText3(length.l)
	Protected *mem, i.i, *text.Byte, text$
	*mem = AllocateMemory(length, #PB_Memory_NoClear)
	*text = *mem
	For i = 1 To length
		*text\b = Random(90) + 32
		*text + 1
	Next
	text$ = PeekS(*mem, length, #PB_Ascii)
	ProcedureReturn text$
EndProcedure
As an example they create these types of strings (respectively):
# a $3. R<c; < #[Q+)> 0 a^* p+ j < 1g < > `tN?%2::
k?1o 1LF i tU4rWXK L uW9 IX7aY36 EYMFKZc1U s aWeom64 6KzI1FI 7YB
p7'rIwiIG<@v3?Yu:<P4OK*bpup9iCf&1N*J.C#Y)*;x*,wC]s&XWhfPqs>H-:(A

It would also be possible to use RandomData and Base64Encode but you would need to allocate some extra memory for the base64 encoding.
Personally I like RandomText3(), it provides Ascii 32 to Ascii 122 range. I did not test if RandomText3() or RandomText1() performs best though.
RandomText1() will look better on a forum (more possibilities of word-wrapping due to the larger number of spaces).
Post Reply