Author Topic: How to efficiently detect ASCII 10 & 13?  (Read 823 times)

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
How to efficiently detect ASCII 10 & 13?
« on: December 21, 2010, 07:13:20 AM »
When you send a HTML form all enters are \n\r (or lone \n depending on system). I want to detect if the text field submitted is empty (nothing or only enter, as a bonus only spaces, but that's not required). How to make it?

The best I came up with is this ugly loop with several calls to ord() function... Althrough the very first iteration should exit from the loop since the legit text should have the first element >32 so maybe it is not that bad...
Code: [Select]
$len=strlen($tmp);
for($n=0;$n<$len;$n++) if(ord($tmp[$n])>32) {$nonblank=1; break;}
if(!$nonblank) {$ok=0; error("Enter something.");}

Offline Harkins

  • Level 28
  • **
  • Posts: 424
  • Reputation: +11/-2
  • Coder, blogger, entrepreneur.
    • View Profile
    • Push CX - Blog
Re: How to efficiently detect ASCII 10 & 13?
« Reply #1 on: December 21, 2010, 09:35:09 AM »
Code: [Select]

if (trim($tmp) == '') {
  error(...);
}

Visit #bbg on irc.freenode.net to talk browser games anytime.

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: How to efficiently detect ASCII 10 & 13?
« Reply #2 on: December 22, 2010, 05:40:04 AM »
Nice. I knew there must be a specialised function for this :D

For the sake of overoptimization:
I think ltrim() would be slightly faster since it has to check the string from one side only and does not need to know the string length.

A question. When we have an overly complex build in function that does a lot of processing we don't need (but compiled to native machine code) or a small and simple code in PHP that does only what it needs to (but processed by slow interpreter), which one should we choose as a rule of thumb? What are your thoughts on this?


Another question (detect max word size):
How to make it faster (I can not use wordwrap because it 'fix' the string while I want to 'validate" the string)?

Code: [Select]
$t=explode(' ',$text);
$count=count($t);
for($n=0;$n<$count;$n++) {if(strlen($t[$n])>80) {$ok=0; error("Maximum word length is 80 characters");}}

Offline Harkins

  • Level 28
  • **
  • Posts: 424
  • Reputation: +11/-2
  • Coder, blogger, entrepreneur.
    • View Profile
    • Push CX - Blog
Re: How to efficiently detect ASCII 10 & 13?
« Reply #3 on: December 22, 2010, 11:18:36 AM »
A question. When we have an overly complex build in function that does a lot of processing we don't need (but compiled to native machine code) or a small and simple code in PHP that does only what it needs to (but processed by slow interpreter), which one should we choose as a rule of thumb? What are your thoughts on this?

Write in PHP. Adding another programming language makes your project much more complex.

Visit #bbg on irc.freenode.net to talk browser games anytime.

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: How to efficiently detect ASCII 10 & 13?
« Reply #4 on: December 22, 2010, 11:27:53 AM »
I believe Chris meant PHP's libraries (which are compiled to C) vs. normal PHP code

@Chris - "a lot of" and "small" are just too vague terms to give any advice, I can't see how we can give any not knowing whether the processing adds 1% or 75% and how much the PHP code takes time etc.
-> where it matters, do benchmark ... where not, it's up to you ... though doing it anyway would give you knowledge about another case (you could store this comparisons somewhere to remember)

Edit: but I agree with nano in most cases the built-in should be much faster
« Last Edit: December 22, 2010, 12:53:01 PM by Nox »
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Marek

  • Level 18
  • *
  • Posts: 177
  • Reputation: +7/-0
  • XHTML, CSS, JS, PHP and MySQL are my pantheon.
    • View Profile
Re: How to efficiently detect ASCII 10 & 13?
« Reply #5 on: December 22, 2010, 11:36:12 AM »
If the question is between *existing* built-in PHP functions versus user-defined PHP functions which are written to be less complex, then in general I believe built-in functions are still going to be faster.

Built-in functions are faster especially with things like looping and manipulating strings. The PHP interpreter introduces a lot of overhead during loops especially. The built-in PHP functions use underlying C libraries, and machine code loops can be orders of magnitude faster than in the interpreter. So I would guess that even the more complex and inefficient functions in C are faster than a short PHP interpreted function.

Offline aerosuidae

  • Level 9
  • *
  • Posts: 50
  • Reputation: +5/-0
    • View Profile
    • Return to Sol
Re: How to efficiently detect ASCII 10 & 13?
« Reply #6 on: January 03, 2011, 06:57:12 AM »
Another question (detect max word size):
How to make it faster (I can not use wordwrap because it 'fix' the string while I want to 'validate" the string)?
Code: [Select]
$t=explode(' ',$text);
$count=count($t);
for($n=0;$n<$count;$n++) {if(strlen($t[$n])>80) {$ok=0; error("Maximum word length is 80 characters");}}

What about a regex looking for 80 or more non-space characters together?

Code: [Select]
if (preg_match('#[^\s]{81,}#', $text))
    error('Maximum word length is 80 characters');
« Last Edit: January 03, 2011, 07:13:35 AM by aerosuidae »

Offline aerosuidae

  • Level 9
  • *
  • Posts: 50
  • Reputation: +5/-0
    • View Profile
    • Return to Sol
Re: How to efficiently detect ASCII 10 & 13?
« Reply #7 on: January 03, 2011, 07:15:36 AM »
The max 80 char thing - I thought of another one:

Code: [Select]
if (max(array_map('strlen', explode(' ', $text))) > 80)
error('Maximum word length is 80 characters');

;D I wonder which is fastest.

Edit: Simple benchmark script shows the regex easily faster. I guess PHP is smart enough to only compile the regex once (yay!).
« Last Edit: January 03, 2011, 07:19:37 AM by aerosuidae »

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal