internal short AddUtf8(string k)
{
int theIndex = itsUtf8Hash.Get(k, -1);
if (theIndex == -1)
{
int strLen = k.Length;
bool tooBigString;
if (strLen > MAX_UTF_ENCODING_SIZE)
{
tooBigString = true;
}
else
{
tooBigString = false;
// Ask for worst case scenario buffer when each char takes 3
// bytes
Ensure(1 + 2 + strLen * 3);
int top = itsTop;
itsPool[top++] = CONSTANT_Utf8;
top += 2;
// skip length
char[] chars = cfw.GetCharBuffer(strLen);
Sharpen.Runtime.GetCharsForString(k, 0, strLen, chars, 0);
for (int i = 0; i != strLen; i++)
{
int c = chars[i];
if (c != 0 && c <= unchecked((int)(0x7F)))
{
itsPool[top++] = unchecked((byte)c);
}
else
{
if (c > unchecked((int)(0x7FF)))
{
itsPool[top++] = unchecked((byte)(unchecked((int)(0xE0)) | (c >> 12)));
itsPool[top++] = unchecked((byte)(unchecked((int)(0x80)) | ((c >> 6) & unchecked((int)(0x3F)))));
itsPool[top++] = unchecked((byte)(unchecked((int)(0x80)) | (c & unchecked((int)(0x3F)))));
}
else
{
itsPool[top++] = unchecked((byte)(unchecked((int)(0xC0)) | (c >> 6)));
itsPool[top++] = unchecked((byte)(unchecked((int)(0x80)) | (c & unchecked((int)(0x3F)))));
}
}
}
int utfLen = top - (itsTop + 1 + 2);
if (utfLen > MAX_UTF_ENCODING_SIZE)
{
tooBigString = true;
}
else
{
// Write back length
itsPool[itsTop + 1] = unchecked((byte)((int)(((uint)utfLen) >> 8)));
itsPool[itsTop + 2] = unchecked((byte)utfLen);
itsTop = top;
theIndex = itsTopIndex++;
itsUtf8Hash.Put(k, theIndex);
}
}
if (tooBigString)
{
throw new ArgumentException("Too big string");
}
}
SetConstantData(theIndex, k);
itsPoolTypes.Put(theIndex, CONSTANT_Utf8);
return (short)theIndex;
}