Special chars in strings

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Special chars in strings

Post by davido »

@Danilo
Nice escape function.
Thank you for sharing. :D
DE AA EB
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Special chars in strings

Post by Shield »

Apart from the fact that the substitution happens at runtime and, even though the code is short and concise it is also highly inefficient. :wink:

The "right" solution would be to have something like this natively implemented using a string prefex, e.g.

Code: Select all

Define a.s = &"Hello\nWorld"
That way existing code is unaffected and substitution is only done if explicitly stated.
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
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Special chars in strings

Post by Danilo »

User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Special chars in strings

Post by Shield »

:wink:

This is what I've come up with. I didn't do any speed testing or serious unit testing,
so I'd be happy if somebody could give me a hand. :P
  1. The main "issue" with Danilo's procedure is that it iterates through the same string multiple times.
  2. I put the replacement logic into a separate procedure in order to speed up cases
    where no escape sequences occur.
  3. I'm not sure if putting a #NUL character into the middle of a string confuses PB,
    but it seems to work so far (possible memory leak?).
  4. The Select statement could be optimized by removing it entirely and replacing
    the insertion character assignment with a lookup table. I don't believe PB's selects actually work that way (I could be wrong).
    In other languages this would be the normal way how select works.

Code: Select all


EnableExplicit

Procedure.i EscapeMove(*current.Character)
	Protected *insert.Character
	
	*insert = *current
	While *current\c <> #NUL
		If *current\c = '~'
			*current + SizeOf(Character)
			Select *current\c
				Case '~' : *current\c = '~'
				Case 'q' : *current\c = '"'
				Case 'n' : *current\c = #LF
				Case 'r' : *current\c = #CR
				Case 't' : *current\c = #TAB
				Case 'v' : *current\c = #VT
				Case '0' : *current\c = #NUL
				Case 'a' : *current\c = #BEL
				Case 'b' : *current\c = #BS
				Case 'f' : *current\c = #FF
				Case #NUL : Break
			EndSelect
		EndIf
		
		*insert\c = *current\c
		*current + SizeOf(Character)
		*insert + SizeOf(Character)
	Wend
	*insert\c = #NUL
	
	ProcedureReturn *current
EndProcedure

Procedure.i EscapeInline(*current.Character)
	While *current\c <> #NUL
		If *current\c = '~'
			ProcedureReturn EscapeMove(*current)
		EndIf
		*current + SizeOf(Character)
	Wend
	
	ProcedureReturn *current
EndProcedure

Procedure.s Escape(string.s)
	EscapeInline(@string)
	ProcedureReturn string
EndProcedure

Debug Escape("Hello~qWorld~q~tWhat's Up?~nNext Line")
Edit: fixed small bug.
Last edited by Shield on Wed Feb 11, 2015 9:10 am, edited 1 time in total.
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
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Special chars in strings

Post by Little John »

And let's not forget mk-soft's sprintf like function:
http://www.purebasic.fr/english/viewtop ... 12&t=32026
Post Reply