本文整理汇总了C++中nsACString::SetCapacity方法的典型用法代码示例。如果您正苦于以下问题:C++ nsACString::SetCapacity方法的具体用法?C++ nsACString::SetCapacity怎么用?C++ nsACString::SetCapacity使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nsACString
的用法示例。
在下文中一共展示了nsACString::SetCapacity方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: GetAddress
/* readonly attribute AUTF8String address; */
NS_IMETHODIMP nsNetAddr::GetAddress(nsACString & aAddress)
{
switch(mAddr.raw.family) {
/* PR_NetAddrToString can handle INET and INET6, but not LOCAL. */
case AF_INET:
aAddress.SetCapacity(kIPv4CStrBufSize);
NetAddrToString(&mAddr, aAddress.BeginWriting(), kIPv4CStrBufSize);
aAddress.SetLength(strlen(aAddress.BeginReading()));
break;
case AF_INET6:
aAddress.SetCapacity(kIPv6CStrBufSize);
NetAddrToString(&mAddr, aAddress.BeginWriting(), kIPv6CStrBufSize);
aAddress.SetLength(strlen(aAddress.BeginReading()));
break;
#if defined(XP_UNIX) || defined(XP_OS2)
case AF_LOCAL:
aAddress.Assign(mAddr.local.path);
break;
#endif
// PR_AF_LOCAL falls through to default when not XP_UNIX || XP_OS2
default:
return NS_ERROR_UNEXPECTED;
}
return NS_OK;
}
示例2:
NS_IMETHODIMP
HttpBaseChannel::GetRemoteAddress(nsACString& addr)
{
if (mPeerAddr.raw.family == PR_AF_UNSPEC)
return NS_ERROR_NOT_AVAILABLE;
addr.SetCapacity(64);
PR_NetAddrToString(&mPeerAddr, addr.BeginWriting(), 64);
addr.SetLength(strlen(addr.BeginReading()));
return NS_OK;
}
示例3: NetAddrToString
NS_IMETHODIMP
HttpBaseChannel::GetLocalAddress(nsACString& addr)
{
if (mSelfAddr.raw.family == PR_AF_UNSPEC)
return NS_ERROR_NOT_AVAILABLE;
addr.SetCapacity(kIPv6CStrBufSize);
NetAddrToString(&mSelfAddr, addr.BeginWriting(), kIPv6CStrBufSize);
addr.SetLength(strlen(addr.BeginReading()));
return NS_OK;
}
示例4: DecodeRFC2047Str
// |decode_mime_part2_str| taken from comi18n.c
// Decode RFC2047-encoded words in the input and convert the result to UTF-8.
// If aOverrideCharset is true, charset in RFC2047-encoded words is
// ignored and aDefaultCharset is assumed, instead. aDefaultCharset
// is also used to convert raw octets (without RFC 2047 encoding) to UTF-8.
//static
nsresult DecodeRFC2047Str(const char *aHeader, const char *aDefaultCharset,
bool aOverrideCharset, nsACString &aResult)
{
const char *p, *q = nullptr, *r;
const char *begin; // tracking pointer for where we are in the input buffer
int32_t isLastEncodedWord = 0;
const char *charsetStart, *charsetEnd;
nsAutoCString prevCharset, curCharset;
nsAutoCString encodedText;
char prevEncoding = '\0', curEncoding;
nsresult rv;
begin = aHeader;
// To avoid buffer realloc, if possible, set capacity in advance. No
// matter what, more than 3x expansion can never happen for all charsets
// supported by Mozilla. SCSU/BCSU with the sliding window set to a
// non-BMP block may be exceptions, but Mozilla does not support them.
// Neither any known mail/news program use them. Even if there's, we're
// safe because we don't use a raw *char any more.
aResult.SetCapacity(3 * strlen(aHeader));
while ((p = PL_strstr(begin, "=?")) != 0) {
if (isLastEncodedWord) {
// See if it's all whitespace.
for (q = begin; q < p; ++q) {
if (!PL_strchr(" \t\r\n", *q)) break;
}
}
if (!isLastEncodedWord || q < p) {
if (!encodedText.IsEmpty()) {
rv = DecodeQOrBase64Str(encodedText.get(), encodedText.Length(),
prevEncoding, prevCharset.get(), aResult);
if (NS_FAILED(rv)) {
aResult.Append(encodedText);
}
encodedText.Truncate();
prevCharset.Truncate();
prevEncoding = '\0';
}
// copy the part before the encoded-word
CopyRawHeader(begin, p - begin, aDefaultCharset, aResult);
begin = p;
}
p += 2;
// Get charset info
charsetStart = p;
charsetEnd = 0;
for (q = p; *q != '?'; q++) {
if (*q <= ' ' || PL_strchr(especials, *q)) {
goto badsyntax;
}
// RFC 2231 section 5
if (!charsetEnd && *q == '*') {
charsetEnd = q;
}
}
if (!charsetEnd) {
charsetEnd = q;
}
q++;
curEncoding = nsCRT::ToUpper(*q);
if (curEncoding != 'Q' && curEncoding != 'B')
goto badsyntax;
if (q[1] != '?')
goto badsyntax;
r = q;
for (r = q + 2; *r != '?'; r++) {
if (*r < ' ') goto badsyntax;
}
if (r[1] != '=')
goto badsyntax;
else if (r == q + 2) {
// it's empty, skip
begin = r + 2;
isLastEncodedWord = 1;
continue;
}
curCharset.Assign(charsetStart, charsetEnd - charsetStart);
// Override charset if requested. Never override labeled UTF-8.
// Use default charset instead of UNKNOWN-8BIT
if ((aOverrideCharset && 0 != nsCRT::strcasecmp(curCharset.get(), "UTF-8"))
|| (aDefaultCharset && 0 == nsCRT::strcasecmp(curCharset.get(), "UNKNOWN-8BIT"))
) {
curCharset = aDefaultCharset;
}
//.........这里部分代码省略.........
示例5: DecodeRFC2047Str
// |decode_mime_part2_str| taken from comi18n.c
// Decode RFC2047-encoded words in the input and convert the result to UTF-8.
// If aOverrideCharset is true, charset in RFC2047-encoded words is
// ignored and aDefaultCharset is assumed, instead. aDefaultCharset
// is also used to convert raw octets (without RFC 2047 encoding) to UTF-8.
//static
nsresult DecodeRFC2047Str(const char *aHeader, const char *aDefaultCharset,
PRBool aOverrideCharset, nsACString &aResult)
{
const char *p, *q, *r;
char *decodedText;
const char *begin; // tracking pointer for where we are in the input buffer
PRInt32 isLastEncodedWord = 0;
const char *charsetStart, *charsetEnd;
char charset[80];
// initialize charset name to an empty string
charset[0] = '\0';
begin = aHeader;
// To avoid buffer realloc, if possible, set capacity in advance. No
// matter what, more than 3x expansion can never happen for all charsets
// supported by Mozilla. SCSU/BCSU with the sliding window set to a
// non-BMP block may be exceptions, but Mozilla does not support them.
// Neither any known mail/news program use them. Even if there's, we're
// safe because we don't use a raw *char any more.
aResult.SetCapacity(3 * strlen(aHeader));
while ((p = PL_strstr(begin, "=?")) != 0) {
if (isLastEncodedWord) {
// See if it's all whitespace.
for (q = begin; q < p; ++q) {
if (!PL_strchr(" \t\r\n", *q)) break;
}
}
if (!isLastEncodedWord || q < p) {
// copy the part before the encoded-word
CopyRawHeader(begin, p - begin, aDefaultCharset, aResult);
begin = p;
}
p += 2;
// Get charset info
charsetStart = p;
charsetEnd = 0;
for (q = p; *q != '?'; q++) {
if (*q <= ' ' || PL_strchr(especials, *q)) {
goto badsyntax;
}
// RFC 2231 section 5
if (!charsetEnd && *q == '*') {
charsetEnd = q;
}
}
if (!charsetEnd) {
charsetEnd = q;
}
// Check for too-long charset name
if (PRUint32(charsetEnd - charsetStart) >= sizeof(charset))
goto badsyntax;
memcpy(charset, charsetStart, charsetEnd - charsetStart);
charset[charsetEnd - charsetStart] = 0;
q++;
if (*q != 'Q' && *q != 'q' && *q != 'B' && *q != 'b')
goto badsyntax;
if (q[1] != '?')
goto badsyntax;
r = q;
for (r = q + 2; *r != '?'; r++) {
if (*r < ' ') goto badsyntax;
}
if (r[1] != '=')
goto badsyntax;
else if (r == q + 2) {
// it's empty, skip
begin = r + 2;
isLastEncodedWord = 1;
continue;
}
if(*q == 'Q' || *q == 'q')
decodedText = DecodeQ(q + 2, r - (q + 2));
else {
// bug 227290. ignore an extraneous '=' at the end.
// (# of characters in B-encoded part has to be a multiple of 4)
PRInt32 n = r - (q + 2);
n -= (n % 4 == 1 && !PL_strncmp(r - 3, "===", 3)) ? 1 : 0;
decodedText = PL_Base64Decode(q + 2, n, nsnull);
}
if (decodedText == nsnull)
//.........这里部分代码省略.........
示例6: lossy_converter
bool
AppendUTF16toUTF8(const nsAString& aSource, nsACString& aDest,
const mozilla::fallible_t& aFallible)
{
// At 16 characters analysis showed better performance of both the all ASCII
// and non-ASCII cases, so we limit calling |FirstNonASCII| to strings of
// that length.
const nsAString::size_type kFastPathMinLength = 16;
int32_t firstNonASCII = 0;
if (aSource.Length() >= kFastPathMinLength) {
firstNonASCII = FirstNonASCII(aSource.BeginReading(), aSource.EndReading());
}
if (firstNonASCII == -1) {
// This is all ASCII, we can use the more efficient lossy append.
mozilla::CheckedInt<nsACString::size_type> new_length(aSource.Length());
new_length += aDest.Length();
if (!new_length.isValid() ||
!aDest.SetCapacity(new_length.value(), aFallible)) {
return false;
}
LossyAppendUTF16toASCII(aSource, aDest);
return true;
}
nsAString::const_iterator source_start, source_end;
CalculateUTF8Size calculator;
aSource.BeginReading(source_start);
aSource.EndReading(source_end);
// Skip the characters that we know are single byte.
source_start.advance(firstNonASCII);
copy_string(source_start,
source_end, calculator);
// Include the ASCII characters that were skipped in the count.
size_t count = calculator.Size() + firstNonASCII;
if (count) {
auto old_dest_length = aDest.Length();
// Grow the buffer if we need to.
mozilla::CheckedInt<nsACString::size_type> new_length(count);
new_length += old_dest_length;
if (!new_length.isValid() ||
!aDest.SetLength(new_length.value(), aFallible)) {
return false;
}
// All ready? Time to convert
nsAString::const_iterator ascii_end;
aSource.BeginReading(ascii_end);
if (firstNonASCII >= static_cast<int32_t>(kFastPathMinLength)) {
// Use the more efficient lossy converter for the ASCII portion.
LossyConvertEncoding16to8 lossy_converter(
aDest.BeginWriting() + old_dest_length);
nsAString::const_iterator ascii_start;
aSource.BeginReading(ascii_start);
ascii_end.advance(firstNonASCII);
copy_string(ascii_start, ascii_end, lossy_converter);
} else {
// Not using the lossy shortcut, we need to include the leading ASCII
// chars.
firstNonASCII = 0;
}
ConvertUTF16toUTF8 converter(
aDest.BeginWriting() + old_dest_length + firstNonASCII);
copy_string(ascii_end,
aSource.EndReading(source_end), converter);
NS_ASSERTION(converter.Size() == count - firstNonASCII,
"Unexpected disparity between CalculateUTF8Size and "
"ConvertUTF16toUTF8");
}
return true;
}