2000-08-05 00:51:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/*
|
|
|
|
* The contents of this file are subject to the Mozilla Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 2000 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Original Author:
|
|
|
|
* Scott Collins <scc@mozilla.org>
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
|
2000-08-05 01:15:45 +00:00
|
|
|
template <class CharT>
|
|
|
|
inline
|
|
|
|
CharT*
|
|
|
|
CopyStringToNewZeroTerminatedBuffer( const basic_nsAReadableString<CharT>& aSourceString )
|
2000-08-05 00:51:37 +00:00
|
|
|
{
|
2000-08-05 01:15:45 +00:00
|
|
|
// no conversion needed, just allocate a buffer of the correct length and copy into it
|
|
|
|
|
|
|
|
CharT* result = NS_STATIC_CAST(CharT*, nsMemory::Alloc( (aSourceString.Length()+1) * sizeof(CharT) ));
|
|
|
|
*copy_string(aSourceString.BeginReading(), aSourceString.EndReading(), result) = CharT(0);
|
|
|
|
return result;
|
2000-08-05 00:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
ToNewCString( const nsAReadableCString& aSourceCString )
|
|
|
|
{
|
2000-08-05 01:15:45 +00:00
|
|
|
return CopyStringToNewZeroTerminatedBuffer(aSourceCString);
|
2000-08-05 00:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PRUnichar*
|
|
|
|
ToNewUnicode( const nsAReadableString& aSourceString )
|
2000-08-05 01:15:45 +00:00
|
|
|
{
|
|
|
|
return CopyStringToNewZeroTerminatedBuffer(aSourceString);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-05 02:13:59 +00:00
|
|
|
template <class FromT, class ToT>
|
|
|
|
class LossyConversionSink
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef FromT input_type;
|
|
|
|
typedef ToT output_type;
|
|
|
|
|
|
|
|
public:
|
|
|
|
LossyConversionSink( output_type* aDestination ) : mDestination(aDestination) { }
|
|
|
|
|
|
|
|
PRUint32
|
|
|
|
write( const input_type* aSource, PRUint32 aSourceLength )
|
|
|
|
{
|
|
|
|
const input_type* done_writing = aSource + aSourceLength;
|
|
|
|
while ( aSource < done_writing )
|
|
|
|
*mDestination++ = NS_STATIC_CAST(output_type, *aSource++);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
OutputCharT* mDestination;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-08-05 01:15:45 +00:00
|
|
|
|
|
|
|
char*
|
|
|
|
ToNewCString( const nsAReadableString& aSourceString )
|
2000-08-05 00:51:37 +00:00
|
|
|
{
|
2000-08-05 02:13:59 +00:00
|
|
|
char* result = NS_STATIC_CAST(char*, nsMemory::Alloc(aSourceString.Length()+1));
|
|
|
|
*copy_string(aSourceString.BeginReading(), aSourceString.EndReading(), LossyConversionSink<PRUnichar, char>(result)) = char(0);
|
|
|
|
return result;
|
2000-08-05 00:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PRUnichar*
|
|
|
|
ToNewUnicode( const nsAReadableCString& aSourceCString )
|
|
|
|
{
|
2000-08-05 02:13:59 +00:00
|
|
|
PRUnichar* result = NS_STATIC_CAST(char*, nsMemory::Alloc( (aSourceString.Length()+1) * sizeof(PRUnichar) ));
|
|
|
|
*copy_string(aSourceCString.BeginReading(), aSourceCString.EndReading(), LossyConversionSink<char, PRUnichar>(result)) = PRUnichar(0);
|
|
|
|
return result;
|
2000-08-05 00:51:37 +00:00
|
|
|
}
|
|
|
|
|
2000-08-05 01:15:45 +00:00
|
|
|
|
|
|
|
|
2000-08-05 00:51:37 +00:00
|
|
|
PRBool
|
|
|
|
IsASCII( const nsAReadableString& aSourceString )
|
|
|
|
{
|
|
|
|
const PRUnichar NOT_ASCII = ~0x007F;
|
|
|
|
|
|
|
|
|
|
|
|
// Don't want to use |copy_string| for this task, since we can stop at the first non-ASCII character
|
|
|
|
|
|
|
|
nsAReadableString::const_iterator iter = aSourceString.BeginReading();
|
|
|
|
nsAReadableString::const_iterator done_reading = aSourceString.EndReading();
|
|
|
|
|
|
|
|
// for each chunk of |aSourceString|...
|
|
|
|
while ( iter != done_reading )
|
|
|
|
{
|
|
|
|
PRUint32 chunk_size = iter.size_forward();
|
|
|
|
const PRUnichar* c = iter.get();
|
|
|
|
const PRUnichar* chunk_end = c + chunk_size;
|
|
|
|
|
|
|
|
// for each character in this chunk...
|
|
|
|
while ( c < chunk_end )
|
|
|
|
if ( *c++ & NOT_ASCII )
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
iter += chunk_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
2000-08-05 02:13:59 +00:00
|
|
|
}
|
|
|
|
|