Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ PunycodeEncode()

static string System.Globalization.IdnMapping.PunycodeEncode ( string unicode)
inlinestaticprivate

Definition at line 246 of file IdnMapping.cs.

247 {
248 if (unicode.Length == 0)
249 {
250 throw new ArgumentException(SR.Argument_IdnBadLabelSize, "unicode");
251 }
252 StringBuilder stringBuilder = new StringBuilder(unicode.Length);
253 int num = 0;
254 int num2 = 0;
255 int num3 = 0;
256 while (num < unicode.Length)
257 {
258 num = unicode.IndexOfAny(s_dotSeparators, num2);
259 if (num < 0)
260 {
261 num = unicode.Length;
262 }
263 if (num == num2)
264 {
265 if (num == unicode.Length)
266 {
267 break;
268 }
269 throw new ArgumentException(SR.Argument_IdnBadLabelSize, "unicode");
270 }
271 stringBuilder.Append("xn--");
272 bool flag = false;
273 StrongBidiCategory bidiCategory = CharUnicodeInfo.GetBidiCategory(unicode, num2);
274 if (bidiCategory == StrongBidiCategory.StrongRightToLeft)
275 {
276 flag = true;
277 int num4 = num - 1;
278 if (char.IsLowSurrogate(unicode, num4))
279 {
280 num4--;
281 }
282 bidiCategory = CharUnicodeInfo.GetBidiCategory(unicode, num4);
283 if (bidiCategory != StrongBidiCategory.StrongRightToLeft)
284 {
285 throw new ArgumentException(SR.Argument_IdnBadBidi, "unicode");
286 }
287 }
288 int num5 = 0;
289 for (int i = num2; i < num; i++)
290 {
291 StrongBidiCategory bidiCategory2 = CharUnicodeInfo.GetBidiCategory(unicode, i);
292 if (flag && bidiCategory2 == StrongBidiCategory.StrongLeftToRight)
293 {
294 throw new ArgumentException(SR.Argument_IdnBadBidi, "unicode");
295 }
296 if (!flag && bidiCategory2 == StrongBidiCategory.StrongRightToLeft)
297 {
298 throw new ArgumentException(SR.Argument_IdnBadBidi, "unicode");
299 }
300 if (Basic(unicode[i]))
301 {
302 stringBuilder.Append(EncodeBasic(unicode[i]));
303 num5++;
304 }
305 else if (char.IsSurrogatePair(unicode, i))
306 {
307 i++;
308 }
309 }
310 int num6 = num5;
311 if (num6 == num - num2)
312 {
313 stringBuilder.Remove(num3, "xn--".Length);
314 }
315 else
316 {
317 if (unicode.Length - num2 >= "xn--".Length && unicode.Substring(num2, "xn--".Length).Equals("xn--", StringComparison.OrdinalIgnoreCase))
318 {
319 throw new ArgumentException(SR.Argument_IdnBadPunycode, "unicode");
320 }
321 int num7 = 0;
322 if (num6 > 0)
323 {
324 stringBuilder.Append('-');
325 }
326 int num8 = 128;
327 int num9 = 0;
328 int num10 = 72;
329 while (num5 < num - num2)
330 {
331 int num11 = 0;
332 int num12 = 134217727;
333 for (int j = num2; j < num; j += ((!IsSupplementary(num11)) ? 1 : 2))
334 {
335 num11 = char.ConvertToUtf32(unicode, j);
336 if (num11 >= num8 && num11 < num12)
337 {
338 num12 = num11;
339 }
340 }
341 num9 += (num12 - num8) * (num5 - num7 + 1);
342 num8 = num12;
343 for (int j = num2; j < num; j += ((!IsSupplementary(num11)) ? 1 : 2))
344 {
345 num11 = char.ConvertToUtf32(unicode, j);
346 if (num11 < num8)
347 {
348 num9++;
349 }
350 if (num11 != num8)
351 {
352 continue;
353 }
354 int num13 = num9;
355 int num14 = 36;
356 while (true)
357 {
358 int num15 = ((num14 <= num10) ? 1 : ((num14 >= num10 + 26) ? 26 : (num14 - num10)));
359 if (num13 < num15)
360 {
361 break;
362 }
363 stringBuilder.Append(EncodeDigit(num15 + (num13 - num15) % (36 - num15)));
364 num13 = (num13 - num15) / (36 - num15);
365 num14 += 36;
366 }
367 stringBuilder.Append(EncodeDigit(num13));
368 num10 = Adapt(num9, num5 - num7 + 1, num5 == num6);
369 num9 = 0;
370 num5++;
371 if (IsSupplementary(num12))
372 {
373 num5++;
374 num7++;
375 }
376 }
377 num9++;
378 num8++;
379 }
380 }
381 if (stringBuilder.Length - num3 > 63)
382 {
383 throw new ArgumentException(SR.Argument_IdnBadLabelSize, "unicode");
384 }
385 if (num != unicode.Length)
386 {
387 stringBuilder.Append('.');
388 }
389 num2 = num + 1;
390 num3 = stringBuilder.Length;
391 }
392 if (stringBuilder.Length > 255 - ((!IsDot(unicode[^1])) ? 1 : 0))
393 {
394 throw new ArgumentException(SR.Format(SR.Argument_IdnBadNameSize, 255 - ((!IsDot(unicode[^1])) ? 1 : 0)), "unicode");
395 }
396 return stringBuilder.ToString();
397 }
static bool IsDot(char c)
static char EncodeDigit(int d)
static readonly char[] s_dotSeparators
Definition IdnMapping.cs:14
static char EncodeBasic(char bcp)
static bool IsSupplementary(int cTest)
static int Adapt(int delta, int numpoints, bool firsttime)
StringBuilder Remove(int startIndex, int length)
override string ToString()
StringBuilder Append(char value, int repeatCount)

References System.Globalization.IdnMapping.Adapt(), System.Text.StringBuilder.Append(), System.SR.Argument_IdnBadBidi, System.SR.Argument_IdnBadLabelSize, System.SR.Argument_IdnBadNameSize, System.SR.Argument_IdnBadPunycode, System.Basic, System.Globalization.IdnMapping.EncodeBasic(), System.Globalization.IdnMapping.EncodeDigit(), System.SR.Format(), System.Globalization.CharUnicodeInfo.GetBidiCategory(), System.Globalization.IdnMapping.IsDot(), System.Globalization.IdnMapping.IsSupplementary(), System.Text.StringBuilder.Length, System.Text.StringBuilder.Remove(), System.Globalization.IdnMapping.s_dotSeparators, and System.Text.StringBuilder.ToString().

Referenced by System.Globalization.IdnMapping.GetAsciiInvariant().