Quote:
Originally Posted by Headline
Why are you so obsessed with regex?
Spoiler
PHP Code:
public Action Command_EnterRGBA(int client, int args) { if (args != 4) { ReplyToCommand(client, "[SM] Usage: sm_rgba <0-255> <0-255> <0-255> <0-255>"); return Plugin_Handled; } if (client == 0 || !IsClientInGame(client)) { ReplyToCommand(client, "[SM] You must be in-game to use this command!"); return Plugin_Handled; }
char str1[4], str2[4], str3[4], str4[4]; GetCmdArg(1, str1, sizeof(str1)); GetCmdArg(2, str2, sizeof(str2)); GetCmdArg(3, str3, sizeof(str3)); GetCmdArg(4, str4, sizeof(str4));
int r = StringToInt(str1); if (!(0 <= r <= 255)) { ReplyToCommand(client, "[SM] Red value is outside the valid range of 0-255."); return Plugin_Handled; } int g = StringToInt(str2); if (!(0 <= g <= 255)) { ReplyToCommand(client, "[SM] Green value is outside the valid range of 0-255."); return Plugin_Handled; }
int b = StringToInt(str3); if (!(0 <= b <= 255)) { ReplyToCommand(client, "[SM] Blue value is outside the valid range of 0-255."); return Plugin_Handled; } int a = StringToInt(str4); if (!(0 <= a <= 255)) { ReplyToCommand(client, "[SM] Alpha value is outside the valid range of 0-255."); return Plugin_Handled; } // use RGBA how you want return Plugin_Handled; }
|
Even this is a little excessive; it's not necessary to hold the user's hand for the range validation, just as it's not necessary to use regex to check for non-numeric characters. Even if they omit an argument, I'd just go with it.
PHP Code:
public Action Command_EnterRGBA(int client, int args) {
if (client == 0 || !IsClientInGame(client)) {
ReplyToCommand(client, "[SM] You must be in-game to use this command!");
return Plugin_Handled;
}
char chRGBA[4][4];
int iRGBA[4];
for (int i; i < sizeof(chRGBA) && i < args; i++) {
GetCmdArg(i+1, chRGBA[i], sizeof(chRGBA[i]));
iRGBA[i] = StringToInt(chRGBA[i]);
if (!(0 <= iRGBA[i] <= 255)) {
iRGBA[i] = 0;
ReplyToCommand(client, "RGBA values must be between 0 and 255.");
}
}
// use RGBA how you want
return Plugin_Handled;
}
__________________