|
X 116
Last post 11-25-2008 8:50 AM by MiffTheFox. 21 replies.
-
11-21-2008 4:36 PM
|
|
-
OSvsOS


- Joined on 07-25-2008
- Posts 35
|
Hello All, I was browsing through some PHP code my predessor wrote and found this gem if( strpos( strtolower( $_SERVER["HTTP_USER_AGENT"] ), "mozilla" ) || strpos( strtolower( $_SERVER["HTTP_USER_AGENT"] ), "firefox" ) || strpos( strtolower( $_SERVER["HTTP_USER_AGENT"] ), "safari" ) ) { //echo '<td align="right" style="position relative; left: -50px;">'; // FF this does not work echo '<td align="center">'; // FF for( $i=0; $i<116; $i++ ) echo " "; } else { echo '<td align="right" style="position relative; left: -38px;">'; // IE } My IDE reports almost 44,000 warnings and 4,500 errors with the entire web site. I am sure to have bigger and brighter gems than this one.
|
|
-
-
-
ElizabethGreene


- Joined on 11-21-2008
- Posts 1
|
A second, subtle, wtf.. All of the IE user agent strings I've seen start with "Mozilla x/0".
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,989
|
ElizabethGreene:A second, subtle, wtf.. All of the IE user agent strings I've seen start with "Mozilla x/0".
strpos() returns the zero-indexed position of the search string. Now, zero-based indexing is stupid (as noted in previous threads) but since zero evaluates as false in PHP, strpos('mozilla...', 'mozilla') will evaluate as false and IE will not enter into the first block. PHP's strpos() is particularly annoying, though, because it returns false if the string is not found (instead of -1), which means you have to do strpos('foo', 'bar') === false to check that the string doesn't actually exist. I usually end up wrapping strpos() so it returns -1 for "not found".
|
|
-
-
MiffTheFox


- Joined on 07-03-2008
- Posts 180
|
morbiuswilters:Now, zero-based indexing is stupid (as noted in previous threads) Flamewar, GO!
|
|
-
-
belgariontheking


- Joined on 08-20-2007
- Cincinnati, OH, USA
- Posts 2,276
|
MiffTheFox: morbiuswilters:Now, zero-based indexing is stupid (as noted in previous threads)
Flamewar, GO!
Not Quite. This has been played out pretty well.
SpectateSwamp exposing aliens. Obviously the World needs SSDS
[10:07] <fatdog> so from now on.. be sure to wear nice clean underwear [10:07] <mps> fatdog: That is simply not going to happen
|
|
-
-
OSvsOS


- Joined on 07-25-2008
- Posts 35
|
I feel it is necessary to reference another piece of code related to strpos. Enjoy...
function any2dec( $num, $base=35, $index=false ) { if( !$base ) { $base = strlen( $index ); } else if( !$index ) { $index = substr( "0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ", 0, $base ); } $out = 0; $len = strlen( $num ) - 1; for( $t = 0; $t <= $len; $t++ ) { $out = $out + strpos( $index, substr( $num, $t, 1 ) ) * pow( $base, $len - $t ); } return $out; }
|
|
-
-
bstorer


- Joined on 02-01-2007
- Alexandria, VA
- Posts 3,402
|
belgariontheking: MiffTheFox: morbiuswilters:Now, zero-based indexing is stupid (as noted in previous threads)
Flamewar, GO!
Not Quite. This has been played out pretty well.
Has not!
|
|
-
-
samanddeanus


- Joined on 04-01-2008
- Posts 73
|
MiffTheFox:
morbiuswilters:Now, zero-based indexing is stupid (as noted in previous threads)
Flamewar, GO!
I propose a compromise: 0.5 based indexing.
Seriously, different tasks call for different conventions (e.g. dates and strings should porbably be one-based)
Donald Knuth:
Who are you? How did you get in my house?
|
|
-
-
OSvsOS


- Joined on 07-25-2008
- Posts 35
|
Indexed arrays and Associative arrays seem to solve all my problems.
|
|
-
-
Physics Phil


- Joined on 03-09-2008
- Posts 118
|
samanddeanus: MiffTheFox:
morbiuswilters:Now, zero-based indexing is stupid (as noted in previous threads)
Flamewar, GO!
I propose a compromise: 0.5 based indexing.
Seriously, different tasks call for different conventions (e.g. dates and strings should porbably be one-based)
ISTR that consnsus was that whether 0 or 1 based indexing is best from a theoretical point of view, in practice the vast majority of programmers have learnt C at some point and are used to 0-based indexing, and so the minor annoyance which is caused by having to remember that this @!*%# language uses 1-based indexing could be the difference between a nice language but which is a little obscure and getting a critical mass of users to make the language viable for commercial use, meaning that all new languages are almost certainly going to choose 0-based indexing. Furthermore, would "not found" be 0 or -1, and what about searching from the end of a list backwards.
|
|
-
-
zzo38


- Joined on 02-10-2008
- Posts 188
|
Physics Phil:....... Furthermore, would "not found" be 0 or -1, and what about searching from the end of a list backwards.
If all numbers are treated as true then "not found" should be the boolean false value, like it is in PHP. But since PHP doesn't treat all numbers as true that is why boolean false value is not found, is sometimes the problem (but is sometimes good it is like that), as noted.
In BASIC, strings start at 1, but arrays can start at whatever you want it to start at.
In Forth you can make anything to start at whatever you want it to start at. Also, if you make a function to search in string, in case it is zero-based, you could make it return two values on the stack if it is successful, boolean true on top and position directly underneath, or just the false value (zero) without anything underneath if it is not found. That way you can make something like (do search in string) IF (do something with found position) THEN you make it according to which way is better for whatever program you are trying to write. If strings are one-based and not-found is zero then you can write something like (do search in string) ?DUP IF (do something with found position) THEN
: IF` 0=GOTO` ORIG ; : THEN` HERE SWAP ! ; : ELSE` GOTO` ORIG SWAP THEN` ; gopher://zzo38computer.cjb.net/
|
|
-
-
MiffTheFox


- Joined on 07-03-2008
- Posts 180
|
zzo38:If all numbers are treated as true then "not found" should be the boolean false value, like it is in PHP. But since PHP doesn't treat all numbers as true that is why boolean false value is not found, is sometimes the problem (but is sometimes good it is like that), as noted. I personally think this "use index function to test presence of string location" is just stupid. The optimal solution is a seperate function that simply checks for the presence of a string, then the index function would just throw an error when using an incorrect value. For example (In C#):
public static bool Contains(this string haystack, string needle){
return haystack.IndexOf(needle) > -1;
}
public static int IndexOfChecked(this string haystack, string needle){
int index = haystack.IndexOf(needle);
if (index >= 0) return index;
else throw new SubstringNotFoundException();
}
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,989
|
MiffTheFox:For example (In C#):
public static bool Contains(this string haystack, string needle){
return haystack.IndexOf(needle) > -1;
}
public static int IndexOfChecked(this string haystack, string needle){
int index = haystack.IndexOf(needle);
if (index >= 0) return index;
else throw new SubstringNotFoundException();
}
You, sir, win +1 Evil Internets.
|
|
-
-
tdb


- Joined on 09-24-2008
- Posts 197
|
MiffTheFox: zzo38:If all numbers are treated as true then "not found" should be the boolean false value, like it is in PHP. But since PHP doesn't treat all numbers as true that is why boolean false value is not found, is sometimes the problem (but is sometimes good it is like that), as noted. I personally think this "use index function to test presence of string location" is just stupid. The optimal solution is a seperate function that simply checks for the presence of a string, then the index function would just throw an error when using an incorrect value. For example (In C#):
public static bool Contains(this string haystack, string needle){ return haystack.IndexOf(needle) > -1; }
public static int IndexOfChecked(this string haystack, string needle){ int index = haystack.IndexOf(needle); if (index >= 0) return index; else throw new SubstringNotFoundException(); }
In many cases it is necessary to look for a substring, then do something on the substring if it was found, and some default action otherwise. For example, Google's search: if a search term contains a colon, check the left side of the colon to see if it's an operator word. If it is, add a special search condition. If there's no colon, add the term to the words to search for. I use similar logic in many of my programs. Using separate contains / index_of_checked functions would cause two linear searches through the string. Here's an alternate proposal (in C++):
using std::string;
bool find(const string &haystack, const string &needle, string::size_type &index) { index = haystack.find(needle); return (index!=string::npos); } The downside is that you'll have to declare an extra variable to store the result in, even if you just wanted to do a "contains" check.
|
|
-
-
-
MiffTheFox


- Joined on 07-03-2008
- Posts 180
|
XIU:the Contains already exists and does the same :p
you could go with a:
public static bool TryIndexOf(this string str, string value, out int index)
{
index = str.IndexOf(value);
return index >= 0;
}
Correction: Contains(char) already exists. Contains(string) would overload it. But yes, a out variable does seem to be the optimal solution.
|
|
-
-
XIU


- Joined on 01-08-2007
- Belgium
- Posts 208
|
Reflector only shows:
public bool Contains(string value)
{
return (this.IndexOf(value, StringComparison.Ordinal) >= 0);
}
on the string class, the only way you would get Contains(char) is with Linq since the string is an IEnumerable<char>
|
|
-
-
MiffTheFox


- Joined on 07-03-2008
- Posts 180
|
XIU:Reflector only shows:
public bool Contains(string value)
{
return (this.IndexOf(value, StringComparison.Ordinal) >= 0);
}
on the string class, the only way you would get Contains(char) is with Linq since the string is an IEnumerable<char> Hmm, MSDN says differently...
|
|
-
-
morbiuswilters


- Joined on 01-15-2008
- East Coast Represent!
- Posts 4,989
|
MiffTheFox: XIU:the Contains already exists and does the same :p
you could go with a:
public static bool TryIndexOf(this string str, string value, out int index)
{
index = str.IndexOf(value);
return index >= 0;
}
Correction: Contains(char) already exists. Contains(string) would overload it. But yes, a out variable does seem to be the optimal solution.
Uhh... why are you guys talking like you are serious?
|
|
-
-
XIU


- Joined on 01-08-2007
- Belgium
- Posts 208
|
MiffTheFox: XIU:Reflector only shows:
public bool Contains(string value)
{
return (this.IndexOf(value, StringComparison.Ordinal) >= 0);
}
on the string class, the only way you would get Contains(char) is with Linq since the string is an IEnumerable<char> Hmm, MSDN says differently...
Yes it does. You are showing the extensions methods, which like I said are coming from the fact that string is an IEnumerable<char>
|
|
-
-
MiffTheFox


- Joined on 07-03-2008
- Posts 180
|
XIU: MiffTheFox: XIU:Reflector only shows:
public bool Contains(string value)
{
return (this.IndexOf(value, StringComparison.Ordinal) >= 0);
}
on the string class, the only way you would get Contains(char) is with Linq since the string is an IEnumerable<char> Hmm, MSDN says differently...
Yes it does. You are showing the extensions methods, which like I said are coming from the fact that string is an IEnumerable<char> Woah, you're right. I just couldn't tell what MSDN was actually showing me. I guess the real WTF is MSDN. </obligitory>
|
|
Page 1 of 1 (22 items)
|
|
|