Page 2 of 2

Re: Constant request: #LOREM$

Posted: Mon Dec 22, 2014 7:04 pm
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.

Re: Constant request: #LOREM$

Posted: Mon Dec 22, 2014 7:26 pm
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...

Re: Constant request: #LOREM$

Posted: Mon Dec 22, 2014 7:59 pm
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

Re: Constant request: #LOREM$

Posted: Mon Dec 22, 2014 8:23 pm
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.

Re: Constant request: #LOREM$

Posted: Mon Dec 22, 2014 9:16 pm
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

Re: Constant request: #LOREM$

Posted: Tue Dec 23, 2014 9:49 am
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.

Re: Constant request: #LOREM$

Posted: Tue Dec 23, 2014 2:03 pm
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:

Re: Constant request: #LOREM$

Posted: Tue Dec 23, 2014 10:13 pm
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 ~

Re: Constant request: #LOREM$

Posted: Mon Dec 29, 2014 8:04 pm
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)...

Re: Constant request: #LOREM$

Posted: Tue Dec 30, 2014 8:46 am
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:

Re: Constant request: #LOREM$

Posted: Tue Dec 30, 2014 9:02 am
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:

Re: Constant request: #LOREM$

Posted: Sat Jan 03, 2015 12:22 am
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 :-)

Re: Constant request: #LOREM$

Posted: Tue Jan 06, 2015 11:53 pm
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).