Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-11-2011, 06:58 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
String and number range checks

This is a function I made while working on my RegEx parser which is soon finished. I thought I would release this so others could perhaps use it for validation and checks etc. Good example: Limit and validation of input
The use of ascii values is pretty obvious,
NPC Code:

if ("a" == "A") {
echo("a equals A");
}



Here's the code. Haven't heavy tested it so might my bugs.
PHP Code:
// V1.0
function onCreated() {
  
this.NR "48,49,50,51,52,53,54,55,56,57";
  
this.az "97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122";
  
this.AZ "65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90";
}
public function 
rangecheck(rangecheckstring) {
  if (
rangecheck.charat(0) == "[")
    
rangecheck rangecheck.substring(1rangecheck.length()-2);
  
temp.hyphens rangecheck.positions("-");
  
temp.ranges = {};
  for(
temp.temp.hyphens) {
    
temp.ranges.add(rangecheck.substring(temp.i-13));
  }
  
temp.check false;
  for(
temp.range temp.ranges) {
    
temp.value1 temp.range.charat(0);
    
temp.value2 temp.range.charat(2);
    
    
temp.array = "error";
    
    if ((
getascii(temp.value1in this.NR) && (getascii(string) in this.NR)) {
      
temp.array = this.NR;
    }
    else if (
getascii(temp.value1in this.az) {
      if ((
getascii(temp.value2in this.az) && (getascii(string) in this.az)) {
        
temp.array = this.az;
      }
    }
    else if ((
getascii(temp.value1in this.AZ) && (getascii(string) in this.AZ)) {
      
temp.array = this.AZ;
    }
    else if (
temp.array == "error") continue;

    
temp.startpoint temp.array.index(getascii(temp.value1));
    
temp.endpoint temp.array.index(getascii(temp.value2));

    
temp.arraycheck temp.array.subarray(
      
temp.startpoint, (temp.endpoint-temp.startpoint+1)
    );
    if (
getascii(string) in temp.arraycheck) {
      
temp.check true;
      break;
    }
  }
  return 
temp.check;
}

public function 
is_nr(input) {
  return (
getascii(inputin this.NR);
}
public function 
is_az(input) {
  return (
getascii(inputin this.az);
}
public function 
is_AZ(input) {
  return (
getascii(inputin this.AZ);

Examples:
PHP Code:
/*
    rangecheck(range, string);

    Takes a range and check if the string character is within this range

    ranges:
      0-9: any number between 0 and 9
      a-z: any character between lowercase a and lowercase z
      A-Z: any character between uppercase A and uppercase Z

      you write ranges like this:
        [0-4a-f]: would check if character is one of 0,1,2,3,4 or a,b,c,d,e,f
        [0-9a-fA-F]: would check if character is between 0 and 9, a and f and A and F
        [1-3a-cA-C]: would check if character is one of 1,2,3,a,b,c,A,B,C

      string: a single character, a letter or number
*/
function onCreated() {
  
this.join("module_regex_rangecheck");

  echo(
rangecheck("[0-9a-f]""f")); // true, f is within the a-f range

__________________

Last edited by Chompy; 12-12-2011 at 04:31 AM.. Reason: See PFA's post beneath ^^ removed lower to upper ranges
Reply With Quote
  #2  
Old 12-11-2011, 08:39 PM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
[a-B] is an invalid regex...
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #3  
Old 12-12-2011, 04:32 AM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by Tolnaftate2004 View Post
[a-B] is an invalid regex...
Thanks for pointing that out haha, didn't realize.
__________________
Reply With Quote
  #4  
Old 12-12-2011, 05:29 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by Chompy View Post
Thanks for pointing that out haha, didn't realize.
Ah, well, it's not that you can't mix lower and upper case in ranges, it's just that B < a lexicographically, so the correct range would be [B-a], which includes B, C, ... Z, [, \, ], ^, _ , `, and finally a.

I think what you wanted for [a-B] is actually [ABa-z].

e: I've done some testing and transliteration to e.g.
PHP Code:
[a-z] -> getascii(cin |getascii("a"), getascii("z")| 
works properly with arbitrary bounds (as long as they are properly parsed*) and even "broken" ranges like [a-B].

*Consider [[-]] and [\[-\]].
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 03:25 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.