bug #54712, r=rjc, a=waterson. Added a |Distance| function to calculate the span between two iterators, then added a constructor and appropriate global function to allow |Substring|s to be made from iterators in addition to the old scheme of {index,length}

This commit is contained in:
scc%mozilla.org 2000-10-05 01:07:02 +00:00
parent e8afc1691f
commit 64e5d4cbd1
9 changed files with 180 additions and 0 deletions

View File

@ -1184,6 +1184,15 @@ class nsPromiseSubstring
// nothing else to do here
}
nsPromiseSubstring( const nsReadingIterator<CharT>& aStart, const nsReadingIterator<CharT>& aEnd )
: mString(*aStart.mOwningString)
{
nsReadingIterator<CharT> zeroPoint;
mOwningString.BeginReading(zeroPoint);
mStartPos = Distance(zeroPoint, aStart);
mLength = Distance(aStart, aEnd);
}
// nsPromiseSubstring( const nsPromiseSubstring<CharT>& ); // auto-generated copy-constructor should be OK
// ~nsPromiseSubstring(); // auto-generated destructor OK
@ -1278,6 +1287,13 @@ Substring( const basic_nsAReadableString<CharT>& aString, PRUint32 aStartPos, PR
return nsPromiseSubstring<CharT>(aString, aStartPos, aSubstringLength);
}
template <class CharT>
nsPromiseSubstring<CharT>
Substring( const nsReadingIterator<CharT>& aStart, const nsReadingIterator<CharT>& aEnd )
{
return nsPromiseSubstring<Chart>(aStart, aEnd);
}
template <class CharT>
int
Compare( const basic_nsAReadableString<CharT>& lhs, const basic_nsAReadableString<CharT>& rhs )

View File

@ -35,6 +35,11 @@
#include "nsAWritableString.h"
#endif
NS_COM size_t Distance( const nsReadingIterator<PRUnichar>&, const nsReadingIterator<PRUnichar>& );
NS_COM size_t Distance( const nsReadingIterator<char>&, const nsReadingIterator<char>& );
NS_COM void CopyUCS2toASCII( const nsAReadableString& aSource, nsAWritableCString& aDest );
NS_COM void CopyASCIItoUCS2( const nsAReadableCString& aSource, nsAWritableString& aDest );

View File

@ -27,6 +27,45 @@
#include "nsCRT.h"
template <class CharT> class CalculateLength
{
public:
typedef CharT value_type;
CalculateLength() : mDistance(0) { }
size_t GetDistance() const { return mDistance; }
PRUint32 write( const CharT*, PRUint32 N )
{ mDistance += N; return N; }
private:
size_t mDistance;
};
template <class CharT>
inline
size_t
Distance_Impl( const nsReadingIterator<CharT>& aStart,
const nsReadingIterator<CharT>& aEnd )
{
CalculateLength<CharT> sink;
nsReadingIterator<CharT> fromBegin(aStart);
copy_string(fromBegin, aEnd, sink);
return sink.GetDistance();
}
NS_COM
size_t
Distance( const nsReadingIterator<PRUnichar>& aStart, const nsReadingIterator<PRUnichar>& aEnd )
{
return Distance_Impl(aStart, aEnd);
}
NS_COM
size_t
Distance( const nsReadingIterator<char>& aStart, const nsReadingIterator<char>& aEnd )
{
return Distance_Impl(aStart, aEnd);
}

View File

@ -1184,6 +1184,15 @@ class nsPromiseSubstring
// nothing else to do here
}
nsPromiseSubstring( const nsReadingIterator<CharT>& aStart, const nsReadingIterator<CharT>& aEnd )
: mString(*aStart.mOwningString)
{
nsReadingIterator<CharT> zeroPoint;
mOwningString.BeginReading(zeroPoint);
mStartPos = Distance(zeroPoint, aStart);
mLength = Distance(aStart, aEnd);
}
// nsPromiseSubstring( const nsPromiseSubstring<CharT>& ); // auto-generated copy-constructor should be OK
// ~nsPromiseSubstring(); // auto-generated destructor OK
@ -1278,6 +1287,13 @@ Substring( const basic_nsAReadableString<CharT>& aString, PRUint32 aStartPos, PR
return nsPromiseSubstring<CharT>(aString, aStartPos, aSubstringLength);
}
template <class CharT>
nsPromiseSubstring<CharT>
Substring( const nsReadingIterator<CharT>& aStart, const nsReadingIterator<CharT>& aEnd )
{
return nsPromiseSubstring<Chart>(aStart, aEnd);
}
template <class CharT>
int
Compare( const basic_nsAReadableString<CharT>& lhs, const basic_nsAReadableString<CharT>& rhs )

View File

@ -27,6 +27,45 @@
#include "nsCRT.h"
template <class CharT> class CalculateLength
{
public:
typedef CharT value_type;
CalculateLength() : mDistance(0) { }
size_t GetDistance() const { return mDistance; }
PRUint32 write( const CharT*, PRUint32 N )
{ mDistance += N; return N; }
private:
size_t mDistance;
};
template <class CharT>
inline
size_t
Distance_Impl( const nsReadingIterator<CharT>& aStart,
const nsReadingIterator<CharT>& aEnd )
{
CalculateLength<CharT> sink;
nsReadingIterator<CharT> fromBegin(aStart);
copy_string(fromBegin, aEnd, sink);
return sink.GetDistance();
}
NS_COM
size_t
Distance( const nsReadingIterator<PRUnichar>& aStart, const nsReadingIterator<PRUnichar>& aEnd )
{
return Distance_Impl(aStart, aEnd);
}
NS_COM
size_t
Distance( const nsReadingIterator<char>& aStart, const nsReadingIterator<char>& aEnd )
{
return Distance_Impl(aStart, aEnd);
}

View File

@ -35,6 +35,11 @@
#include "nsAWritableString.h"
#endif
NS_COM size_t Distance( const nsReadingIterator<PRUnichar>&, const nsReadingIterator<PRUnichar>& );
NS_COM size_t Distance( const nsReadingIterator<char>&, const nsReadingIterator<char>& );
NS_COM void CopyUCS2toASCII( const nsAReadableString& aSource, nsAWritableCString& aDest );
NS_COM void CopyASCIItoUCS2( const nsAReadableCString& aSource, nsAWritableString& aDest );

View File

@ -1184,6 +1184,15 @@ class nsPromiseSubstring
// nothing else to do here
}
nsPromiseSubstring( const nsReadingIterator<CharT>& aStart, const nsReadingIterator<CharT>& aEnd )
: mString(*aStart.mOwningString)
{
nsReadingIterator<CharT> zeroPoint;
mOwningString.BeginReading(zeroPoint);
mStartPos = Distance(zeroPoint, aStart);
mLength = Distance(aStart, aEnd);
}
// nsPromiseSubstring( const nsPromiseSubstring<CharT>& ); // auto-generated copy-constructor should be OK
// ~nsPromiseSubstring(); // auto-generated destructor OK
@ -1278,6 +1287,13 @@ Substring( const basic_nsAReadableString<CharT>& aString, PRUint32 aStartPos, PR
return nsPromiseSubstring<CharT>(aString, aStartPos, aSubstringLength);
}
template <class CharT>
nsPromiseSubstring<CharT>
Substring( const nsReadingIterator<CharT>& aStart, const nsReadingIterator<CharT>& aEnd )
{
return nsPromiseSubstring<Chart>(aStart, aEnd);
}
template <class CharT>
int
Compare( const basic_nsAReadableString<CharT>& lhs, const basic_nsAReadableString<CharT>& rhs )

View File

@ -35,6 +35,11 @@
#include "nsAWritableString.h"
#endif
NS_COM size_t Distance( const nsReadingIterator<PRUnichar>&, const nsReadingIterator<PRUnichar>& );
NS_COM size_t Distance( const nsReadingIterator<char>&, const nsReadingIterator<char>& );
NS_COM void CopyUCS2toASCII( const nsAReadableString& aSource, nsAWritableCString& aDest );
NS_COM void CopyASCIItoUCS2( const nsAReadableCString& aSource, nsAWritableString& aDest );

View File

@ -27,6 +27,45 @@
#include "nsCRT.h"
template <class CharT> class CalculateLength
{
public:
typedef CharT value_type;
CalculateLength() : mDistance(0) { }
size_t GetDistance() const { return mDistance; }
PRUint32 write( const CharT*, PRUint32 N )
{ mDistance += N; return N; }
private:
size_t mDistance;
};
template <class CharT>
inline
size_t
Distance_Impl( const nsReadingIterator<CharT>& aStart,
const nsReadingIterator<CharT>& aEnd )
{
CalculateLength<CharT> sink;
nsReadingIterator<CharT> fromBegin(aStart);
copy_string(fromBegin, aEnd, sink);
return sink.GetDistance();
}
NS_COM
size_t
Distance( const nsReadingIterator<PRUnichar>& aStart, const nsReadingIterator<PRUnichar>& aEnd )
{
return Distance_Impl(aStart, aEnd);
}
NS_COM
size_t
Distance( const nsReadingIterator<char>& aStart, const nsReadingIterator<char>& aEnd )
{
return Distance_Impl(aStart, aEnd);
}