mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Nov 2004
Posts: 842
Jigsy Offline OP
Hoopy frood
OP Offline
Hoopy frood
Joined: Nov 2004
Posts: 842
So I recently began rescripting an IRCd in mIRC for the first time in over a decade.

Anyway, I was trying to convert an ident to legal characters (a-z, A-Z, 0-9, - _ and .) using the following code:

Code:
alias -l legalIdentChar {
  ; $legalIdentChar(<char>)

  if ($pos(abcdefghijklmnopqrstuvwxyz1234567890-.,$1,1) > 0) { return $1 }
  else { return _ }
  ; `-> _ is also a valid character, but it's easier to sweep it under this.
}
alias -l legalizeIdent {
  ; $legalizeIdent(<args>)

  return $regsubex($1,/(.)/g,$legalIdentChar(\t))
}


But I noticed this:

Code:
$legalizeIdent(J)  > J
$legalizeIdent(J@) > J_

^-> Are fine, but...

$legalizeIdent(£)  > __
$legalizeIdent(<random kanji which the forum converted>) > ___


Any reason why it's returning more than one _ for a single character? Or this is a bug?


What do you do at the end of the world? Are you busy? Will you save us?
Joined: Jan 2004
Posts: 2,127
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2004
Posts: 2,127
You need the 'u' flag dealing with characters above codepoint 127.

Code:
//echo -a $regsubex($chr(233),/(.)/g ,x) is xx
//echo -a $regsubex($chr(233),/(.)/gu,x) is x

Joined: Nov 2004
Posts: 842
Jigsy Offline OP
Hoopy frood
OP Offline
Hoopy frood
Joined: Nov 2004
Posts: 842
Thanks. :3


What do you do at the end of the world? Are you busy? Will you save us?
Joined: Feb 2003
Posts: 2,812
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
What is the purpose of this function, if I may ask? To make sure only allowed characters are used in a string? This will happen a lot over the course of a program, and it can be done with just a single $regsubex() without needing two specialized functions.

Code:
var %user_ident = $lower($1)
var %user_ident = $regsubex(%user_ident,/([^a-z0-9_.-])/gu,_)

Simple string sanitation.


Well. At least I won lunch.
Good philosophy, see good in bad, I like!
Joined: Nov 2004
Posts: 842
Jigsy Offline OP
Hoopy frood
OP Offline
Hoopy frood
Joined: Nov 2004
Posts: 842
My knowledge of regex is surprisingly limited.


What do you do at the end of the world? Are you busy? Will you save us?

Link Copied to Clipboard