Pointer to String Constant

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Demivec
Addict
Addict
Posts: 4090
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Pointer to String Constant

Post by Demivec »

Code: Select all

#String1 = "Constant String X"
#String2 = "Constant String X"
#String3 = "Constant String 3"

Procedure test(*StringConstant)
  Debug Str(*StringConstant) + ": " + PeekS(*StringConstant)
EndProcedure

Define myString.s = "Variable String"
test(@myString)
test(@"Literal String")
test(?string1)
test(?string2)
test(?string3)

DataSection
  String1: Data.s #String1
  String2: Data.s #String2
  String3: Data.s #String3
EndDataSection

@Edit: shortened example code
Last edited by Demivec on Mon Nov 07, 2011 2:22 pm, edited 1 time in total.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Pointer to String Constant

Post by Shield »

Well if you're going to do something complicated like this, you might as well just go with:

Code: Select all

Macro MyConst
	"Hello World"
EndMacro
#MyConst = MyConst

Procedure Pointer(*pointer)
	
	Debug PeekS(*pointer)
	ProcedureReturn #True
EndProcedure

Debug #MyConst
Debug MyConst
Pointer(@MyConst)
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
skywalk
Addict
Addict
Posts: 3995
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Pointer to String Constant

Post by skywalk »

Bump :idea:
myStringProcedure(@#MYConst$) would have made string library changes much simpler, instead of:

Code: Select all

;- Pointer to a string constant. --> @#MYCONST$
Prototype.i PtrToStr(aString.p-unicode)
Procedure.i PtrToStr(aString.i)
  ProcedureReturn aString
EndProcedure
Global atS.PtrToStr = @PtrToStr()
CompilerIf 1
  Procedure SF_ShowPtrContents(*p)
    ; Work with ByRef string. Not its copy.
    Debug PeekS(*p)
  EndProcedure
  #String1 = "StrConst 1"
  String2$ = "StrVar 2"
  SF_ShowPtrContents(atS(#String1))    ;<-- Annoying for String Constants.
  SF_ShowPtrContents(@String2$)
  SF_ShowPtrContents(@"Literal String")
CompilerEndIf
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Pointer to String Constant

Post by Mistrel »

Blood wrote:Why would you need a pointer to a constant though?
It saves memory and processing power by being able to reuse the pointer to a string without having to allocate memory and perform and assignment each time you want to use it somewhere. For example, error messages.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Pointer to String Constant

Post by Sicro »

Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Pointer to String Constant

Post by walbus »

It should work, it's useful
Post Reply