How do fight a Crazy node name?

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 02 February 2013   #1
How to fight a Crazy node name?

the max allows to name a node with probably no a limitations... for many years i used some 'insure' method to make a node's name executable...
let's say the node is named THE NODE. to execute this name i can do:

     execute ("$'" + node.name + "'")
     

well... but what if the name is [THE NODE]*, or worse \[THE' 'NODE]\*?
simple single-quotation does work anymore.
ok... we can add "\" before every special literal. the name will look like \\\[THE\' \'NODE\]\\\*
that will work.

the simple function that might ensure the name can be:

     fn getInsureName node = 
     (
     	name = substitutestring node.name "\\" "\\\\" 
     	name = substitutestring name "*" "\*"
     	name = substitutestring name "[" "\["	
     	name = substitutestring name "]" "\]"
     	name = substitutestring name "'" "\'"
     	"'" + name + "'"
     )
     

that is a list of known by me special literals... may be there are some more. but the function already looks ugly.

does anyone see a smarter way?

edit
here is some cleaner version but it's still ugly:

fn getInsureName node = 
  (
   	literals = "\*[]'"
   	name = node.name
   	for k=1 to literals.count do name = substitutestring name literals[k] ("\\" + literals[k]) 
   	"'" + name + "'"
   )

Last edited by denisT : 02 February 2013 at 06:30 PM.
 
Old 02 February 2013   #2
that's what i said... i forgot the question mark... so literals are \*[]'? now
 
Old 02 February 2013   #3
I might be overlooking something (has to be the case), as for me simple getNodeByName call works for all these cases (provided that the \ is properly escaped (max 9 here, no verbatim string) )
__________________
Scripts :: linkedin

Last edited by Swordslayer : 02 February 2013 at 06:43 PM.
 
Old 02 February 2013   #4
Originally Posted by Swordslayer: I might be overlooking something (has to be the case), as for me simple getNodeByName call works for all these cases (provided that the \ is properly escaped (max 9 here, no verbatim string) )

getNodeByName works well if need a single node. i need sometimes a pathname, like all namesake nodes, or node with children, or a property path... in short, everything what PathName is for.
 
Old 02 February 2013   #5
Hmm, (dotNetClass "System.Text.RegularExpressions.Regex").Escape catches some, not all though (and many undesired too)... Intriguing question.
__________________
Scripts :: linkedin

Last edited by Swordslayer : 02 February 2013 at 07:06 PM.
 
Old 02 February 2013   #6
Originally Posted by Swordslayer: many undesired...

... is the real problem. i can make the Regex but with the Regex the function will look worse
 
Old 02 February 2013   #7
Yeah, fugly... (dotNetClass "System.Text.RegularExpressions.Regex").Replace str "[\[\]\\\*'?]" "\\$0"
__________________
Scripts :: linkedin
 
Old 02 February 2013   #8
Originally Posted by Swordslayer: Yeah, fugly... (dotNetClass "System.Text.RegularExpressions.Regex").Replace str "[\[\]\\\*'?]" "\\$0"

actually it's not too bad! i kinda like it
 
Old 02 February 2013   #9
so we have a leader:

struct NodeNameExt 
(
private
	regex = dotNetClass "System.Text.RegularExpressions.Regex",
	pattern = "[\[\]\\\*'?]", 
	replacement = "\\$0",
public	
	fn insureName node = 
	(
		"'" + (regex.Replace node.name pattern replacement) + "'"
	)
)
/*
nne = NodeNameExt()
nne.insureName <node>
*/


which is a little slower than 'substitutestring' used version but... and it's very important... the regex version in the structure eats 4 time less memory
 
Old 02 February 2013   #10
let's continue have a fun

struct NodeNameExt 
(
private
	regexClass = dotnetclass "System.Text.RegularExpressions.Regex",
	pattern = "[\[\]\\\*'?]", 
	replacement = "\\$0",
	stringClass = dotnetclass "System.String",
	format = "{1}{0}{1}",
public
	fn doubleEnd name end:"'" = stringClass.Format format name end,
	fn insureName node = (doubleEnd (regexClass.Replace node.name pattern replacement))
)
 
Old 02 February 2013   #11
On a different but still related note. I much prefer to capture all potential illegal characters at source. ie: use callbacks to capture any dodgy user input and this is the key - BEFORE it can do any damage further downstream in the pipeline.
Of course, you need to have exceptions such as ".Target" must be allowed :-)
 
Old 02 February 2013   #12
we used to do the same, it was a big stick we used to beat the artists with.
 
Old 02 February 2013   #13
nope... string.format is worse than mxs:

 struct NodeNameExt 
 (
 private
 	regexClass = dotnetclass "System.Text.RegularExpressions.Regex",
 	pattern = "[\[\]\\\*'?]", 
 	replacement = "\\$0",
 public
 	fn doubleEnd name end:"'" = end + name + end,
 	fn insureName name = (doubleEnd (regexClass.Replace name pattern replacement)),
 	fn executableName name = ("$" + insureName name)
 )
 
 
Old 02 February 2013   #14
Originally Posted by Klunk: we used to do the same, it was a big stick we used to beat the artists with.

i agree. artist always finds a way to break the rule.
 
Old 02 February 2013   #15
fun is continuing...

  global NodeNameExt
  (
  	struct NodeNameStruct
  	(
  	private
  		regexClass = dotnetclass "System.Text.RegularExpressions.Regex",
  		pattern = "[\[\]\\\*'?]", 
  		replacement = "\\$0",
  	public
  		fn doubleEnd name end:"'" = end + name + end,
  		fn insureName name = (doubleEnd (regexClass.Replace name pattern replacement)),
  		fn executableName name = ("$" + insureName name),
  		fn isLegal name = not (regexClass.IsMatch name pattern),
  		fn findIllegal nodes = 
  		(
  			for node in nodes where not isLegal node.name collect node
  		)
  	)
  	NodeNameExt = NodeNameStruct()
  	ok
  )
  

Last edited by denisT : 02 February 2013 at 08:42 PM.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 07:13 PM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.