String ccomparison problem. How to convert AUTF8String (nsACString) to a regular String or vice versa

Hi, I'm having trouble comparing strings of different types in Java
Script
The two strings are equal when displayed in an alert popup but the
"==" condition fails

The two types are the following
1.
nsIURI.spec
http://developer.mozilla.org/en/docs/nsIURI#Attributes

and

2.
http://developer.mozilla.org/en/docs/nsIScriptableInputStream:read



i receive a URL through nsIScriptableInputStream.read() and attempt to
compare it with nsIURI.spec

In this sample code, i pass in the nsIScriptableInputStream.read() as
the "url" parameter, which is then compared to :
http://developer.mozilla.org/en/docs/Code_snippets:Tabbed_browser#Reusing_tabs
I print out a alert message of the "url" and
currentBrowser.currentURI.spec and they are equal according to the
alert pop up but the "==" condition fails.

I have tried using this interface to convert the Read in string to
autf8string and then compare with nsIURI.spec but no luck:
http://www.xulplanet.com/references/xpcomref/ifaces/nsIUTF8ConverterService.html

should the above work?
0
EricL84
3/12/2008 5:32:29 PM
mozilla.dev.extensions 5694 articles. 0 followers. Post Follow

7 Replies
1488 Views

Similar Articles

[PageSpeed] 9
Get it on Google Play
Get it on Apple App Store

EricL84@gmail.com wrote:
> i receive a URL through nsIScriptableInputStream.read() and attempt to
> compare it with nsIURI.spec

A better approach would be to use the IO service to convert the input to 
a URI 
<http://lxr.mozilla.org/mozilla/source/netwerk/base/public/nsIIOService.idl#75> 
and then use nsIURI::equals 
<http://lxr.mozilla.org/mozilla/source/netwerk/base/public/nsIURI.idl#173> 
to compare the two:

let ioService = Components.classes["@mozilla.org/network/io-service;1"].
                 getService(Components.interfaces.nsIIOService);
let uri = ioService.newURI(myScriptableInputStream.read(), null, null);
uri.equals(currentBrowser.currentURI);

-myk
0
Myk
3/12/2008 8:45:42 PM
On Mar 12, 1:45 pm, Myk Melez <m...@mozilla.org> wrote:
> Eric...@gmail.com wrote:
> > i receive a URL through nsIScriptableInputStream.read() and attempt to
> > compare it with nsIURI.spec
>
> A better approach would be to use the IO service to convert the input to
> a URI
> <http://lxr.mozilla.org/mozilla/source/netwerk/base/public/nsIIOServic...>
> and then use nsIURI::equals
> <http://lxr.mozilla.org/mozilla/source/netwerk/base/public/nsIURI.idl#173>
> to compare the two:
>
> let ioService = Components.classes["@mozilla.org/network/io-service;1"].
>                  getService(Components.interfaces.nsIIOService);
> let uri = ioService.newURI(myScriptableInputStream.read(), null, null);
> uri.equals(currentBrowser.currentURI);
>
> -myk

Thank you myk.
I had actually implemented your method previously, but it seems that
if the data read() in does not contain "http://" it causes in error.
This was a reason i wanted to avoid converting the input to URI.  Is
there a way around this?

However, I found the reason for the failed comparison between the
strings.  The data read in contained an "\n"  end of line character!

Thanks
Eric
0
EricL84
3/12/2008 10:51:44 PM
EricL84@gmail.com wrote:
> I had actually implemented your method previously, but it seems that
> if the data read() in does not contain "http://" it causes in error.
> This was a reason i wanted to avoid converting the input to URI.  Is
> there a way around this?

If you know that strings without a scheme are http:// URLs, you could 
simply check for the presence of a scheme and prepend "http://" if it's 
missing.  This might be as simple as catching an exception thrown by 
newURI (if it throws such an exception) and retrying.

-myk
0
Myk
3/12/2008 11:23:09 PM
On Mar 12, 4:23 pm, Myk Melez <m...@mozilla.org> wrote:
> Eric...@gmail.com wrote:
> > I had actually implemented your method previously, but it seems that
> > if the data read() in does not contain "http://" it causes in error.
> > This was a reason i wanted to avoid converting the input to URI.  Is
> > there a way around this?
>
> If you know that strings without a scheme are http:// URLs, you could
> simply check for the presence of a scheme and prepend "http://" if it's
> missing.  This might be as simple as catching an exception thrown by
> newURI (if it throws such an exception) and retrying.
>
> -myk

I actually implemented a prependHTTP() functions as well ;D.  But
there is a slight problem with this because it is possible that a URL/
domain may use HTTPS instead of HTTP and I needed the entire URL to
match.  Is there a way (a function/API) to find out what the scheme is
for a domain (URL)? (Sorry if im misusing the terms, not quite
familiar with them).

Thanks so much
Eric
0
EricL84
3/13/2008 12:41:55 AM
EricL84@gmail.com wrote:
> Is there a way (a function/API) to find out what the scheme is
> for a domain (URL)? (Sorry if im misusing the terms, not quite
> familiar with them).

No, as any given domain could have resources addressed by multiple 
schemes.  For example, the following URLs are all valid completions of 
the "URL" example.com/foo:

   http://example.com/foo
   https://example.com/foo
   ftp://example.com/foo
   irc://example.com/foo

But you might be able to sniff out a scheme by sending HEAD requests for 
each possible scheme in turn until you get a positive response.

-myk
0
Myk
3/13/2008 1:37:09 AM
Myk Melez wrote:
> EricL84@gmail.com wrote:
>> Is there a way (a function/API) to find out what the scheme is
>> for a domain (URL)? (Sorry if im misusing the terms, not quite
>> familiar with them).
> 
> No, as any given domain could have resources addressed by multiple
> schemes.  For example, the following URLs are all valid completions of
> the "URL" example.com/foo:
> 
>   http://example.com/foo
>   https://example.com/foo
>   ftp://example.com/foo
>   irc://example.com/foo
> 
> But you might be able to sniff out a scheme by sending HEAD requests for
> each possible scheme in turn until you get a positive response.
> 
> -myk

Uncontrolled sniffing around might be a bad thing, considering
especially privacy issues arising from it. Not to mention that some
grumpy sysadmin might consider this a portscan/breakin attempt. :p

There is however, nsIURIFixup which the location bar uses to do lookups...
http://mxr.mozilla.org/mozilla1.8/source/docshell/base/nsIURIFixup.idl
It will "guess" the correct scheme without actively sniffing... like
www.example.org => http, or ftp.example.org => ftp

Might worth having a look at it.

Nils
0
Nils
3/13/2008 1:46:52 AM
On Mar 12, 6:46 pm, Nils Maier <Maier...@web.de> wrote:
> Myk Melez wrote:
> > Eric...@gmail.com wrote:
> >> Is there a way (a function/API) to find out what the scheme is
> >> for a domain (URL)? (Sorry if im misusing the terms, not quite
> >> familiar with them).
>
> > No, as any given domain could have resources addressed by multiple
> > schemes.  For example, the following URLs are all valid completions of
> > the "URL" example.com/foo:
>
> >  http://example.com/foo
> >  https://example.com/foo
> >  ftp://example.com/foo
> >   irc://example.com/foo
>
> > But you might be able to sniff out a scheme by sending HEAD requests for
> > each possible scheme in turn until you get a positive response.
>
> > -myk
>
> Uncontrolled sniffing around might be a bad thing, considering
> especially privacy issues arising from it. Not to mention that some
> grumpy sysadmin might consider this a portscan/breakin attempt. :p
>
> There is however, nsIURIFixup which the location bar uses to do lookups...http://mxr.mozilla.org/mozilla1.8/source/docshell/base/nsIURIFixup.idl
> It will "guess" the correct scheme without actively sniffing... likewww.example.org=> http, or ftp.example.org => ftp
>
> Might worth having a look at it.
>
> Nils

Thanks for the tip on nsIURIFixup, this seems safer than prepending
http:// "manually".
However, im having trouble with a particular case of using
nsIURIFixup.  When I pass a domain to URIFixup, it prepends http://.
This website first goes to the http:// URL and then is redirected to a
httpS:// URL.  I am using the pageshow event and upon this event
firing, the final URL in the location bar is the httpS:// URL.  Is
there a way to find the final (redirected?) protocol and URL from a
domain?

Thanks
0
EricL84
3/14/2008 5:15:20 AM
Reply:

Similar Artilces:

string and string[]
hello, whats the difference here between string and string[] thanks "string" denotes an item of type String "string[]" denotes an array of type String itemsThanks, EdMicrosoft MVP - ASP/ASP.NET Gracia mon ami...

Convert from string to integer and vice versa
I’m new to Visual Basic and am developing my first web application with VB. It’s based on at skeleton script from my host provider. It’s a form where users fill in some data, which I validate with the VB script, before the data are sent with an e-mail. I’ve mostly been able to turn the skeleton script into an application which suits my needs, but I’ve run into some problems with code which should be trivial. I need to convert from string to integer and vice versa. I’ve been searching the web, and this forum, for information on how to do this. I’ve found several ways to convert...

String and String[]
What will be the advantage of using myString: String[10] over myString: String Man wrote: > What will be the advantage of using > myString: String[10] > over > myString: String "String[10]" is a fixed-length, statically allocated, Ansi-based ShortString. "String" is a dynamic-length, dynamically allocated, reference counted, Ansi-based or Unicode-based (depening on Delphi version) Long string. Unless you absolutely need the static, fixed length nature of ShortString, such as in records that interact with external systems, you should stay ...

string to string[]
Hi, I've got this code : string[] Params; string SQL = "SELECT * FROM T_MANAGEMENT_PAGES"; SqlCommand myCommand = new SqlCommand(SQL, myConnection); myConnection.Open(); SqlDataReader myReader = myCommand.ExecuteReader(); try { while (myReader.Read()) { Params_Type = myReader.GetValue(0).ToString(); } } catch { } finally { } myReader.Close(); My problem is to obtain Params_Type. But each time, it says : "impossible to convert '[object]' in 'string[]' &quo...

string() = string()
this code does not work (not instance of object) Dim Items(), Other() As String Items = New String() {"A","B","C"} Other = New String() {} Other = Items how can I get all the values ? angiras Hi, If I understand your requirement correctly, this should do it Dim Items() As String = {"A", "B", "C"} Dim Other(Items.Length - 1) As String Items.CopyTo(Other, 0) To more closely match you code above, but this will be less efficient Dim Items(), Other() As String ...

Convert String into byte and vice versa
Hi, I want to know how can i convert byte array into string and then that string again back to the same equivalent byte array. I tried this: string str = Convert.ToBase64String(cipherText); byte[] sampleVar = Encoding.ASCII.GetBytes(str); But it seems that sampleVar is not equivalent to cipherText which is actually of the type byte[] :(. Any help... I am actually using the thing in TDES encryption where i have cipherText stored as string then i load it to string variable and need to convert it nto byte array again .. but it gives bad data. If i dont do the conversion ......

String and string?
Hi guys, I was wondering the difference between small letter string type and a capital letter String type. Does anyone know about this? Highly appreciate for your answers! Thankz! The "proper" version is String, as it references the System.String class of the .NET Framework Class Library. The ability to use string is provided by C#, where it is an alias to String. Similarly, the "proper" version of an integer is Int32, as it references System.Int32.  The ability to use int is provided by C#, and the ability to use Integer is provided by VB.NET, and both int and Integer are aliases...

converting arrayname to string or vice-versa
Hi, This one is puzzling me. I know it's in the faq, to not use variables for variable naming, but I find it odd that I can't get a "stringified" form of a variable name, maybe from the symbol table? Or from the B line of modules? Say I have an array like: @somename = (1,2,3,4,5); and I want to write that array to a file, but I want the file named automatically by just dropping the @ off of the @somename. How would you go about doing it? Plus, I would like to be using strict. Also the reverse: take a filename like "somename" and load it to an a...

convert octet string to string
I'm trying to use IDM 3.5.1 provision an AD environmnet with users from eDir. We wanna provision as much info as possible, but one attribute gives us headaches. This is an auxiliary attribute associated with each user in eDir that has it's syntax set to octet string. Problem is we need to provision this over to another attribute in AD that uses string as its syntax. Is thee a way through XPath or ECMA script to have the value converted during the provisioning? I've no experience with either XPath or ECMA so please be detailed in your replies. Thx -- jdoeconsu...

Convert string to string array.
Hi, We have a string array argument in datawindow object for 'IN' sql statement. Our user enter a string in a single line edit. How to convert the string to a string array for the argument of datawindow? ex. user input : 'A', 'B', 'C' => str_arr[]={'A', 'B', 'C'} dw_1.retrieve(str_arr) Thanks in advance. Take a look at the n_cst_string object in the pfc. It has a method called of_ParseToArray that does exactly what you're looking for. It's a pretty simple task to 'port' the code ...

Converting a list of string into string
When I try the following code in Prism I get the error "Error 8 (CE15) Type mismatch, cannot assign array of System.String to System.String " ***************************************************************** var mobileNumberList :List<string> mobNumberArray :string Begin mobileNumberList.add('1234567890'); mobNumberArray := mobileNumberList.ToArray();//error here end; *************************************************************** What needs to be done to convert an array of strings to a string Venkatesh Venkatesh VT a écrit : > When I...

How to convert a String into a UTF8 String
Hello! I want to convert a normal String into a UTF8 String for example:myString = BüromyUTF8String = Büros  I dont know how to do this. In PHP you can do this with myUTF8String = utf8_encode(myString) Thank you for all hints! try this string test ="ÁÉÍÓÚ áéíóú àèìòù äëïöü Ññ €"; byte[] a= System.Text.Encoding.UTF8.GetBytes(test); string test2= System.Text.Encoding.UTF8.GetString(a);  greetingsBest Regards,Sebastián DopicoBlog Desarrollador Thank you Sebastián! If I make it like that - test2 looks the same as test.But I think my problem is solved. (Someone tol...

Concatenate date string & time string & convert to UK DateTime Problem
Hi, I have a date string (from textbox) & a time string (eg "09:00:00") which i want to concatenate together to form a full date time value which is passed into my sql server table. However, after i concatenate the strings & use the convert.toDateTime i always get the US date format (which causes problems with my date comparisons). i want it showing the UK date/time format. I tried inserting the following line in my web.config file on my LOCAL machine but with no luck:<globalization culture="en-GB" uiCulture="en-GB" requestEncoding="...

Convert UPPERCASE string to Upper-Lower Case (ie. THIS IS MY STRING -> This Is My String)
I recently had the need to convert an all capitalized string to upper/lower case, but couldn't find anything to do it in XSLT 1.0.  If it helps anyone out, I wrote the following:   Call it using: <xsl:template match="Ads">  <xsl:call-template name="ToUpperLowerCase">   <xsl:with-param name="inputString" select="YOURSTRINGHERE"/>   <xsl:with-param name="index" select="0"/>   <xsl:with-param name="loopCount" select="st...

Web resources about - String ccomparison problem. How to convert AUTF8String (nsACString) to a regular String or vice versa - mozilla.dev.extensions

Code 313 - Lance Henderson.com
Code 313 Users Manual Code 313 is a calculator for interpreting industrial radiographs in accordance with ASME B31.3. Please read this description ...

Resources last updated: 12/17/2015 11:44:51 PM