28 #include <nsStringAPI.h>
49 sbFraction(PRUint32 aNumerator, PRUint32 aDenominator = 1) :
50 mNumerator(aNumerator),
51 mDenominator(aDenominator) {}
58 PRUint32 aDenominator) :
59 mNumerator(aNumerator + aDenominator * aWholeNumber),
60 mDenominator(aDenominator) {}
71 return ((mNumerator == aOther.mNumerator &&
72 mDenominator == aOther.mDenominator) ||
73 (mNumerator * aOther.mDenominator ==
74 mDenominator * aOther.mNumerator));
85 return *
this < aOther;
91 operator double()
const
93 return static_cast<double>(mNumerator) / static_cast<double>(mDenominator);
101 return (PRInt64)mNumerator * aFraction.mDenominator >
102 (PRInt64)aFraction.mNumerator * mDenominator;
110 return (PRInt64)mNumerator * aFraction.mDenominator <
111 (PRInt64)aFraction.mNumerator * mDenominator;
119 return (PRInt64)mNumerator * aFraction.mDenominator >=
120 (PRInt64)aFraction.mNumerator * mDenominator;
128 return (PRInt64)mNumerator * aFraction.mDenominator <=
129 (PRInt64)aFraction.mNumerator * mDenominator;
150 PRUint32& aNumerator,
151 PRUint32& aDenominator)
const
153 aWhole = mNumerator / mDenominator;
154 aNumerator = mNumerator - (aWhole * aDenominator);
155 aDenominator = mDenominator;
159 PRUint32 mDenominator;
197 return aLeft != aRight && !aLeft.
IsLessThan(aRight);
207 return aLeft == aRight || aLeft > aRight;
217 return aLeft == aRight || aLeft < aRight;
228 result.AppendInt(aFraction.
Numerator(), 10);
230 result.Append(NS_LITERAL_STRING(
"/"));
245 nsDependentSubstring part(aString, aStart, aLength);
246 PRInt32 temp = part.ToInteger(&rv, 10);
247 NS_ENSURE_SUCCESS(rv, rv);
248 NS_ENSURE_TRUE(temp >= 0, NS_ERROR_FAILURE);
265 PRUint32 numerator = 0;
266 PRUint32 denominator = 1;
268 PRInt32
const space = aString.Find(NS_LITERAL_STRING(
" "));
269 PRInt32
const slash = aString.Find(NS_LITERAL_STRING(
"/"));
274 numerator = aString.ToInteger(&rv, 10);
279 NS_ENSURE_SUCCESS(rv, rv);
282 aString.Length() - slash - 1,
284 NS_ENSURE_SUCCESS(rv, rv);
286 aFraction =
sbFraction(numerator, denominator);
292 return NS_ERROR_FAILURE;
296 NS_ENSURE_SUCCESS(rv, rv);
299 NS_ENSURE_SUCCESS(rv, rv);
303 aString.Length() - slash - 1,
305 NS_ENSURE_SUCCESS(rv, rv);
307 aFraction =
sbFraction(whole, numerator, denominator);
bool operator==(sbFraction const &aLeft, sbFraction const &aRight)
bool IsLessThan(sbFraction const &aOther) const
PRBool operator<=(const sbFraction &aFraction)
nsresult sbFractionFromString(nsAString const &aString, sbFraction &aFraction)
bool operator>(sbFraction const &aLeft, sbFraction const &aRight)
PRBool operator>(const sbFraction &aFraction)
PRUint32 Numerator() const
PRBool operator>=(const sbFraction &aFraction)
bool operator<(sbFraction const &aLeft, sbFraction const &aRight)
bool operator<=(sbFraction const &aLeft, sbFraction const &aRight)
PRBool operator<(const sbFraction &aFraction)
bool operator!=(sbFraction const &aLeft, sbFraction const &aRight)
PRUint32 Denominator() const
void GetProperFraction(PRUint32 &aWhole, PRUint32 &aNumerator, PRUint32 &aDenominator) const
sbFraction(PRUint32 aWholeNumber, PRUint32 aNumerator, PRUint32 aDenominator)
bool IsEqual(sbFraction const &aOther) const
sbFraction(PRUint32 aNumerator, PRUint32 aDenominator=1)
nsString sbFractionToString(sbFraction const &aFraction)
nsresult sbFractionParsePart(nsAString const &aString, PRInt32 aStart, PRInt32 aLength, PRUint32 *aValue)
bool operator>=(sbFraction const &aLeft, sbFraction const &aRight)