/// <summary> /// 判断字符串是否是中文 /// </summary> public static bool IsZn(string input) { int code = 0; int chfrom = Convert.ToInt32("4e00", 16); //范围(0x4e00~0x9fff)转换成int(chfrom~chend) int chend = Convert.ToInt32("9fff", 16); if (input != "") { //code = Char.ConvertToUtf32(input, index);//参数 待处理字符串,长度 code = Char.ConvertToUtf32(input, 16); //获得字符串input中指定索引index处字符unicode编码 if (code >= chfrom && code <= chend) { return(true); //当code在中文范围内返回true } else { return(false); //当code不在中文范围内返回false } } return(false); }
public override bool Fallback(char charUnknownHigh, char charUnknownLow, int index) { if (!Char.IsHighSurrogate(charUnknownHigh)) { throw new ArgumentOutOfRangeException("charUnknownHigh", Environment.GetResourceString("ArgumentOutOfRange_Range", 0xD800, 0xDBFF)); } if (!Char.IsLowSurrogate(charUnknownLow)) { throw new ArgumentOutOfRangeException("CharUnknownLow", Environment.GetResourceString("ArgumentOutOfRange_Range", 0xDC00, 0xDFFF)); } int iTemp = Char.ConvertToUtf32(charUnknownHigh, charUnknownLow); // Fall back our char throw new EncoderFallbackException( Environment.GetResourceString("Argument_InvalidCodePageConversionIndex", iTemp, index), charUnknownHigh, charUnknownLow, index); }
private unsafe static bool IsSortable(char *text, int length) { int index = 0; UnicodeCategory uc; while (index < length) { if (Char.IsHighSurrogate(text[index])) { if (index == length - 1 || !Char.IsLowSurrogate(text[index + 1])) { return(false); // unpaired surrogate } uc = CharUnicodeInfo.InternalGetUnicodeCategory(Char.ConvertToUtf32(text[index], text[index + 1])); if (uc == UnicodeCategory.PrivateUse || uc == UnicodeCategory.OtherNotAssigned) { return(false); } index += 2; continue; } if (Char.IsLowSurrogate(text[index])) { return(false); // unpaired surrogate } uc = CharUnicodeInfo.GetUnicodeCategory(text[index]); if (uc == UnicodeCategory.PrivateUse || uc == UnicodeCategory.OtherNotAssigned) { return(false); } index++; } return(true); }
public static IEnumerable <Codepoint> Codepoints(this string s) { for (int i = 0; i < s.Length; ++i) { if (Char.IsHighSurrogate(s[i])) { if (s.Length < i + 2) { throw new InvalidEncodingException(); } if (!char.IsLowSurrogate(s[i + 1])) { throw new InvalidEncodingException(); } yield return(new Codepoint(Char.ConvertToUtf32(s[i], s[++i]))); } else { yield return(new Codepoint((int)s[i])); } } }
public static __std_core._maybe <__std_core._Tuple2_ <int, __std_core._sslice> > SliceNext(__std_core._sslice slice) { if (slice.len <= 0) { return(__std_core._maybe <__std_core._Tuple2_ <int, __std_core._sslice> > .Nothing_); } char c = slice.str[slice.start]; int n = 1; if (Char.IsHighSurrogate(c) && slice.len > 1) { char lo = slice.str[slice.start + 1]; if (Char.IsLowSurrogate(lo)) { c = (char)Char.ConvertToUtf32(slice.str, slice.start); n = 2; } } return(new __std_core._maybe <__std_core._Tuple2_ <int, __std_core._sslice> >( new __std_core._Tuple2_ <int, __std_core._sslice>( (int)c, new __std_core._sslice(slice.str, slice.start + n, slice.len - n)))); }
static public bool IsCantainChinese(string iText) { if (string.IsNullOrEmpty(iText)) { return(false); } int _Code = 0; int _CHStart = Convert.ToInt32("4e00", 16); int _CHEnd = Convert.ToInt32("9fff", 16); for (int i = 0; i < iText.Length; ++i) { _Code = Char.ConvertToUtf32(iText, i); if (_Code >= _CHStart && _Code <= _CHEnd) { return(true); } } return(false); }
/// <summary> /// //截取字符串中文 字母 /// </summary> /// <param name="content">源字符串</param> /// <param name="length">截取长度!</param> /// <returns></returns> public static string SubTrueString(object content, int length) { string strContent = NoHTML(content.ToString()); bool isConvert = false; int splitLength = 0; int currLength = 0; int code = 0; int chfrom = Convert.ToInt32("4e00", 16); //范围(0x4e00~0x9fff)转换成int(chfrom~chend) int chend = Convert.ToInt32("9fff", 16); for (int i = 0; i < strContent.Length; i++) { code = Char.ConvertToUtf32(strContent, i); if (code >= chfrom && code <= chend) { currLength += 2; //中文 } else { currLength += 1;//非中文 } splitLength = i + 1; if (currLength >= length) { isConvert = true; break; } } if (isConvert) { return(strContent.Substring(0, splitLength)); } else { return(strContent); } }
public string GetUnicodeEscapeFromString(string str) { var sb = new StringBuilder(); var chars = str.ToCharArray(); for (var i = 0; i < chars.Length; i++) { if (Char.IsSurrogate(chars[i])) { int codepoint = Char.ConvertToUtf32(chars[i], chars[i + 1]); var codepointStr = "\\U" + codepoint.ToString("X8"); // C#のユニコードエスケープ表記(UTF-32) sb.Append(codepointStr); i++; } else { int codepoint = (int)chars[i]; var codepointStr = "\\u" + codepoint.ToString("X4"); // C#のユニコードエスケープ表記(UTF-16) sb.Append(codepointStr); } } return(sb.ToString()); }
public static int[] ToCodePoints(this string value) { List <int> codePoints; if (value == null) { throw new ArgumentNullException("str"); } codePoints = new List <int>(value.Length); for (int i = 0; i < value.Length; i++) { codePoints.Add(Char.ConvertToUtf32(value, i)); if (Char.IsHighSurrogate(value[i])) { i += 1; } } return(codePoints.ToArray()); }
public override bool Fallback(char charUnknownHigh, char charUnknownLow, int index) { if (!Char.IsHighSurrogate(charUnknownHigh)) { throw new ArgumentOutOfRangeException(nameof(charUnknownHigh), SR.Format(SR.ArgumentOutOfRange_Range, 0xD800, 0xDBFF)); } if (!Char.IsLowSurrogate(charUnknownLow)) { throw new ArgumentOutOfRangeException("CharUnknownLow", SR.Format(SR.ArgumentOutOfRange_Range, 0xDC00, 0xDFFF)); } Contract.EndContractBlock(); int iTemp = Char.ConvertToUtf32(charUnknownHigh, charUnknownLow); // Fall back our char throw new EncoderFallbackException( SR.Format(SR.Argument_InvalidCodePageConversionIndex, iTemp, index), charUnknownHigh, charUnknownLow, index); }
public string GetCodePointsFromString(string str) { var sb = new StringBuilder(); var chars = str.ToCharArray(); for (var i = 0; i < chars.Length; i++) { int codepoint; if (Char.IsSurrogate(chars[i])) { // TODO: 2文字目有無やサロゲートペアかの検証が必要 codepoint = Char.ConvertToUtf32(chars[i], chars[i + 1]); i++; } else { codepoint = (int)chars[i]; } var codepointStr = "U+" + codepoint.ToString("X4"); // 4桁~5桁 sb.Append(codepointStr); } return(sb.ToString()); }
//private static bool CheckCNFromString(string CString) //{ // bool BoolValue = false; // for (int i = 0; i < CString.Length; i++) // { // if (Convert.ToInt32(Convert.ToChar(CString.Substring(i, 1))) < Convert.ToInt32(Convert.ToChar(128))) // { // BoolValue = false; // } // else // { // return BoolValue = true; // } // } // return BoolValue; //} private static bool CheckCNFromString(string CString) { int code = 0; int chfrom = Convert.ToInt32("4e00", 16); //范围(0x4e00~0x9fff)转换成int(chfrom~chend) int chend = Convert.ToInt32("9fff", 16); bool BoolValue = false; for (int i = 0; i < CString.Length; i++) { code = Char.ConvertToUtf32(CString, i); //获得字符串CString中指定索引index处字符unicode编码 if (code >= chfrom && code <= chend) { return(true); //当code在中文范围内返回true } else { BoolValue = false; } } return(BoolValue); }
public int GetUnicodeCodePoint(Char[] chars) { if (chars.Length > 2) { throw new ArgumentException("The array has too many characters."); } if (chars.Length == 2) { if (!Char.IsSurrogatePair(chars[0], chars[1])) { throw new ArgumentException("The array must contain a low and a high surrogate."); } else { return(Char.ConvertToUtf32(chars[0], chars[1])); } } else { return(Char.ConvertToUtf32(chars.ToString(), 0)); } }
/// <summary> /// Inserta todos los nodos de texto y control necesarios para representar un texto determinado. /// </summary> /// <param name="text">Texto a insertar.</param> private void InsertText(string text) { int i = 0; int code = 0; while (i < text.Length) { code = Char.ConvertToUtf32(text, i); if (code >= 32 && code < 128) { StringBuilder s = new StringBuilder(""); while (i < text.Length && code >= 32 && code < 128) { s.Append(text[i]); i++; if (i < text.Length) { code = Char.ConvertToUtf32(text, i); } } mainGroup.AppendChild(new RtfTreeNode(RtfNodeType.Text, s.ToString(), false, 0)); } else { byte[] bytes = encoding.GetBytes(new char[] { text[i] }); mainGroup.AppendChild(new RtfTreeNode(RtfNodeType.Control, "'", true, bytes[0])); i++; } } }
public async Task Jumbo(string emoji) { string emojiUrl = null; if (Emote.TryParse(emoji, out Emote found)) { emojiUrl = found.Url; } else { int codepoint = Char.ConvertToUtf32(emoji, 0); string codepointHex = codepoint.ToString("X").ToLower(); emojiUrl = $"https://raw.githubusercontent.com/twitter/twemoji/gh-pages/2/72x72/{codepointHex}.png"; } try { HttpClient client = new HttpClient(); var req = await client.GetStreamAsync(emojiUrl); await Context.Channel.SendFileAsync(req, Path.GetFileName(emojiUrl), Context.User.Mention); try { await Context.Message.DeleteAsync(); } catch (HttpRequestException) { Log.Information("Couldn't delete message after jumbofying."); } } catch (HttpRequestException) { await ReplyAsync($"Sorry {Context.User.Mention}, I don't recognize that emoji."); } }
public static Obj StrToObj(string str) { int len = str.Length; int[] chars = new int[len]; int count = 0; for (int i = 0; i < len; i++) { Char ch = str[i]; if (Char.IsHighSurrogate(ch)) { i++; if (i < len) { Char ch2 = str[i]; if (Char.IsLowSurrogate(ch2)) { chars[count++] = Char.ConvertToUtf32(ch, ch2); } else { throw new Exception("Invalid string: " + str); } } else { throw new Exception("Invalid string: " + str); } } else { chars[count++] = Convert.ToInt32(ch); } } return(Builder.CreateTaggedObj(SymbObj.StringSymbId, Builder.CreateSeq(chars, count))); }
public static int[] CodePoints(string str) { int len = str.Length; int[] codePoints = new int[len]; int count = 0; for (int i = 0; i < len; i++) { Char ch = str[i]; if (Char.IsHighSurrogate(ch)) { i++; if (i < len) { Char ch2 = str[i]; if (Char.IsLowSurrogate(ch2)) { codePoints[count++] = Char.ConvertToUtf32(ch, ch2); } else { throw new Exception("Invalid string: " + str); } } else { throw new Exception("Invalid string: " + str); } } else { codePoints[count++] = Convert.ToInt32(ch); } } return(count == len ? codePoints : Array.Take(codePoints, count)); }
//而需要判斷是否為中文字 //方法很簡單重點只有一個 中文字起始範圍是0x4E00-0x9FFF //判斷指定字串內的指定位置是否為中文字 private bool IsChinese(string strInputString, int intIndexNumber) { int intCode = 0; //中文範圍(0x4e00 - 0x9fff)轉換成int(intChineseFrom - intChineseEnd) int intChineseFrom = Convert.ToInt32("4e00", 16); int intChineseEnd = Convert.ToInt32("9fff", 16); if (strInputString != "") { //取得input字串中指定判斷的index字元的unicode碼 intCode = Char.ConvertToUtf32(strInputString, intIndexNumber); if (intCode >= intChineseFrom && intCode <= intChineseEnd) { return(true); //如果是範圍內的數值就回傳true } else { return(false); //如果是範圍外的數值就回傳true } } return(false); }
public static void WriteLdmlText(XmlWriter writer, string text) { // Not all Unicode characters are valid in an XML document, so we need to create // the <cp hex="X"> elements to replace the invalid characters. // Note: While 0xD (carriage return) is a valid XML character, it is automatically // either dropped or coverted to 0xA by any conforming XML parser, so we also make a <cp> // element for that one. StringBuilder sb = new StringBuilder(text.Length); for (int i = 0; i < text.Length; i++) { int code = Char.ConvertToUtf32(text, i); if ((code == 0x9) || (code == 0xA) || (code >= 0x20 && code <= 0xD7FF) || (code >= 0xE000 && code <= 0xFFFD) || (code >= 0x10000 && code <= 0x10FFFF)) { sb.Append(Char.ConvertFromUtf32(code)); } else { writer.WriteString(sb.ToString()); writer.WriteStartElement("cp"); writer.WriteAttributeString("hex", String.Format("{0:X}", code)); writer.WriteEndElement(); sb = new StringBuilder(text.Length - i); } if (Char.IsSurrogatePair(text, i)) { i++; } } writer.WriteString(sb.ToString()); }
public static string UrlEncode([CanBeNull] string data, [CanBeNull] Encoding charset, bool spaceAsPlus = false) { var ret = new StringBuilder(); foreach (string ps in StringUtil.StringToCodePointStrings(data)) { if (ps.Length == 1 && UrlSafeChars.Contains(ps[0])) { // URL-safe character ret.Append(ps[0]); } else if (spaceAsPlus && ps.Length == 1 && ps[0] == ' ') { ret.Append('+'); } else { // character in the server's encoding? try { // URL-encode foreach (var b in charset.GetBytes(ps)) { ret.AppendFormat("%{0:X2}", (int)b); } } catch (EncoderFallbackException) { // unsupported natively by the encoding; perform a URL-encoded HTML escape ret.AppendFormat("%26%23{0}%3B", Char.ConvertToUtf32(ps, 0)); } } } return(ret.ToString()); }
// Fallback Methods public override bool Fallback(char charUnknown, int index) { // If we had a buffer already we're being recursive, throw, it's probably at the suspect // character in our array. if (_fallbackCount >= 1) { // If we're recursive we may still have something in our buffer that makes this a surrogate if (char.IsHighSurrogate(charUnknown) && _fallbackCount >= 0 && char.IsLowSurrogate(_strDefault[_fallbackIndex + 1])) { ThrowLastCharRecursive(Char.ConvertToUtf32(charUnknown, _strDefault[_fallbackIndex + 1])); } // Nope, just one character ThrowLastCharRecursive(unchecked ((int)charUnknown)); } // Go ahead and get our fallback // Divide by 2 because we aren't a surrogate pair _fallbackCount = _strDefault.Length / 2; _fallbackIndex = -1; return(_fallbackCount != 0); }
/* PunycodeEncode() converts Unicode to Punycode. The input */ /* is represented as an array of Unicode code points (not code */ /* units; surrogate pairs are not allowed), and the output */ /* will be represented as an array of ASCII code points. The */ /* output string is *not* null-terminated; it will contain */ /* zeros if and only if the input contains zeros. (Of course */ /* the caller can leave room for a terminator and add one if */ /* needed.) The input_length is the number of code points in */ /* the input. The output_length is an in/out argument: the */ /* caller passes in the maximum number of code points that it */ /* can receive, and on successful return it will contain the */ /* number of code points actually output. The case_flags array */ /* holds input_length boolean values, where nonzero suggests that */ /* the corresponding Unicode character be forced to uppercase */ /* after being decoded (if possible), and zero suggests that */ /* it be forced to lowercase (if possible). ASCII code points */ /* are encoded literally, except that ASCII letters are forced */ /* to uppercase or lowercase according to the corresponding */ /* uppercase flags. If case_flags is a null pointer then ASCII */ /* letters are left as they are, and other code points are */ /* treated as if their uppercase flags were zero. The return */ /* value can be any of the punycode_status values defined above */ /* except punycode_bad_input; if not punycode_success, then */ /* output_size and output might contain garbage. */ static string PunycodeEncode(string unicode) { // 0 length strings aren't allowed if (unicode.Length == 0) { throw new ArgumentException(SR.Argument_IdnBadLabelSize, nameof(unicode)); } StringBuilder output = new StringBuilder(unicode.Length); int iNextDot = 0; int iAfterLastDot = 0; int iOutputAfterLastDot = 0; // Find the next dot while (iNextDot < unicode.Length) { // Find end of this segment iNextDot = unicode.IndexOfAny(c_Dots, iAfterLastDot); Contract.Assert(iNextDot <= unicode.Length, "[IdnMapping.punycode_encode]IndexOfAny is broken"); if (iNextDot < 0) { iNextDot = unicode.Length; } // Only allowed to have empty . section at end (www.microsoft.com.) if (iNextDot == iAfterLastDot) { // Only allowed to have empty sections as trailing . if (iNextDot != unicode.Length) { throw new ArgumentException(SR.Argument_IdnBadLabelSize, nameof(unicode)); } // Last dot, stop break; } // We'll need an Ace prefix output.Append(c_strAcePrefix); // Everything resets every segment. bool bRightToLeft = false; // Check for RTL. If right-to-left, then 1st & last chars must be RTL BidiCategory eBidi = CharUnicodeInfo.GetBidiCategory(unicode, iAfterLastDot); if (eBidi == BidiCategory.RightToLeft || eBidi == BidiCategory.RightToLeftArabic) { // It has to be right to left. bRightToLeft = true; // Check last char int iTest = iNextDot - 1; if (Char.IsLowSurrogate(unicode, iTest)) { iTest--; } eBidi = CharUnicodeInfo.GetBidiCategory(unicode, iTest); if (eBidi != BidiCategory.RightToLeft && eBidi != BidiCategory.RightToLeftArabic) { // Oops, last wasn't RTL, last should be RTL if first is RTL throw new ArgumentException(SR.Argument_IdnBadBidi, nameof(unicode)); } } // Handle the basic code points int basicCount; int numProcessed = 0; // Num code points that have been processed so far (this segment) for (basicCount = iAfterLastDot; basicCount < iNextDot; basicCount++) { // Can't be lonely surrogate because it would've thrown in normalization Debug.Assert(Char.IsLowSurrogate(unicode, basicCount) == false, "[IdnMapping.punycode_encode]Unexpected low surrogate"); // Double check our bidi rules BidiCategory testBidi = CharUnicodeInfo.GetBidiCategory(unicode, basicCount); // If we're RTL, we can't have LTR chars if (bRightToLeft && testBidi == BidiCategory.LeftToRight) { // Oops, throw error throw new ArgumentException(SR.Argument_IdnBadBidi, nameof(unicode)); } // If we're not RTL we can't have RTL chars if (!bRightToLeft && (testBidi == BidiCategory.RightToLeft || testBidi == BidiCategory.RightToLeftArabic)) { // Oops, throw error throw new ArgumentException(SR.Argument_IdnBadBidi, nameof(unicode)); } // If its basic then add it if (Basic(unicode[basicCount])) { output.Append(EncodeBasic(unicode[basicCount])); numProcessed++; } // If its a surrogate, skip the next since our bidi category tester doesn't handle it. else if (Char.IsSurrogatePair(unicode, basicCount)) { basicCount++; } } int numBasicCodePoints = numProcessed; // number of basic code points // Stop if we ONLY had basic code points if (numBasicCodePoints == iNextDot - iAfterLastDot) { // Get rid of xn-- and this segments done output.Remove(iOutputAfterLastDot, c_strAcePrefix.Length); } else { // If it has some non-basic code points the input cannot start with xn-- if (unicode.Length - iAfterLastDot >= c_strAcePrefix.Length && unicode.Substring(iAfterLastDot, c_strAcePrefix.Length).Equals( c_strAcePrefix, StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException(SR.Argument_IdnBadPunycode, nameof(unicode)); } // Need to do ACE encoding int numSurrogatePairs = 0; // number of surrogate pairs so far // Add a delimiter (-) if we had any basic code points (between basic and encoded pieces) if (numBasicCodePoints > 0) { output.Append(c_delimiter); } // Initialize the state int n = c_initialN; int delta = 0; int bias = c_initialBias; // Main loop while (numProcessed < (iNextDot - iAfterLastDot)) { /* All non-basic code points < n have been */ /* handled already. Find the next larger one: */ int j; int m; int test = 0; for (m = c_maxint, j = iAfterLastDot; j < iNextDot; j += IsSupplementary(test) ? 2 : 1) { test = Char.ConvertToUtf32(unicode, j); if (test >= n && test < m) { m = test; } } /* Increase delta enough to advance the decoder's */ /* <n,i> state to <m,0>, but guard against overflow: */ delta += (int)((m - n) * ((numProcessed - numSurrogatePairs) + 1)); Debug.Assert(delta > 0, "[IdnMapping.cs]1 punycode_encode - delta overflowed int"); n = m; for (j = iAfterLastDot; j < iNextDot; j += IsSupplementary(test) ? 2 : 1) { // Make sure we're aware of surrogates test = Char.ConvertToUtf32(unicode, j); // Adjust for character position (only the chars in our string already, some // haven't been processed. if (test < n) { delta++; Contract.Assert(delta > 0, "[IdnMapping.cs]2 punycode_encode - delta overflowed int"); } if (test == n) { // Represent delta as a generalized variable-length integer: int q, k; for (q = delta, k = c_punycodeBase; ; k += c_punycodeBase) { int t = k <= bias ? c_tmin : k >= bias + c_tmax ? c_tmax : k - bias; if (q < t) { break; } Debug.Assert(c_punycodeBase != t, "[IdnMapping.punycode_encode]Expected c_punycodeBase (36) to be != t"); output.Append(EncodeDigit(t + (q - t) % (c_punycodeBase - t))); q = (q - t) / (c_punycodeBase - t); } output.Append(EncodeDigit(q)); bias = Adapt(delta, (numProcessed - numSurrogatePairs) + 1, numProcessed == numBasicCodePoints); delta = 0; numProcessed++; if (IsSupplementary(m)) { numProcessed++; numSurrogatePairs++; } } } ++delta; ++n; Debug.Assert(delta > 0, "[IdnMapping.cs]3 punycode_encode - delta overflowed int"); } } // Make sure its not too big if (output.Length - iOutputAfterLastDot > c_labelLimit) { throw new ArgumentException(SR.Argument_IdnBadLabelSize, nameof(unicode)); } // Done with this segment, add dot if necessary if (iNextDot != unicode.Length) { output.Append('.'); } iAfterLastDot = iNextDot + 1; iOutputAfterLastDot = output.Length; } // Throw if we're too long if (output.Length > c_defaultNameLimit - (IsDot(unicode[unicode.Length - 1]) ? 0 : 1)) { throw new ArgumentException(SR.Format(SR.Argument_IdnBadNameSize, c_defaultNameLimit - (IsDot(unicode[unicode.Length - 1]) ? 0 : 1)), nameof(unicode)); } // Return our output string return(output.ToString()); }
public void TestConvertToUtf32Fail1() { Char.ConvertToUtf32('A', '\uDC00'); }
public float TextBounds(float x, float y, StringSegment str, ref Bounds bounds) { var state = GetState(); var q = new FontGlyphSquad(); FontGlyph *glyph = null; var prevGlyphIndex = -1; var isize = (short)(state.Size * 10.0f); var iblur = (short)state.Blur; float scale = 0; Font font; float startx = 0; float advance = 0; float minx = 0; float miny = 0; float maxx = 0; float maxy = 0; if (state.Font < 0 || state.Font >= _fontsNumber) { return(0); } font = _fonts[state.Font]; if (font.Data == null) { return(0); } scale = font.FontInfo.__tt_getPixelHeightScale(isize / 10.0f); y += GetVertAlign(font, state.Align, isize); minx = maxx = x; miny = maxy = y; startx = x; for (int i = 0; i < str.Length; i += Char.IsSurrogatePair(str.String, i + str.Location) ? 2 : 1) { var codepoint = Char.ConvertToUtf32(str.String, i + str.Location); glyph = GetGlyph(font, codepoint, isize, iblur, FONS_GLYPH_BITMAP_OPTIONAL); if (glyph != null) { GetQuad(font, prevGlyphIndex, glyph, scale, state.Spacing, ref x, ref y, &q); if (q.X0 < minx) { minx = q.X0; } if (q.X1 > maxx) { maxx = q.X1; } if ((_params_.Flags & FONS_ZERO_TOPLEFT) != 0) { if (q.Y0 < miny) { miny = q.Y0; } if (q.Y1 > maxy) { maxy = q.Y1; } } else { if (q.Y1 < miny) { miny = q.Y1; } if (q.Y0 > maxy) { maxy = q.Y0; } } } prevGlyphIndex = glyph != null ? glyph->Index : -1; } advance = x - startx; if ((state.Align & Alignment.Left) != 0) { } else if ((state.Align & Alignment.Right) != 0) { minx -= advance; maxx -= advance; } else if ((state.Align & Alignment.Center) != 0) { minx -= advance * 0.5f; maxx -= advance * 0.5f; } bounds.b1 = minx; bounds.b2 = miny; bounds.b3 = maxx; bounds.b4 = maxy; return(advance); }
public static Int32 ConvertToUtf32(this String s, Int32 index) { return(Char.ConvertToUtf32(s, index)); }
public void TestConvertToUtf32() { Assert.AreEqual(0x10000, Char.ConvertToUtf32('\uD800', '\uDC00'), "#1"); Assert.AreEqual(0x10FFFF, Char.ConvertToUtf32('\uDBFF', '\uDFFF'), "#2"); }
/// <summary> /// Encodes the given text using Punycode. /// </summary> public static String Encode(String text) { const Int32 InitialBias = 72; const Int32 InitialNumber = 0x80; const Int32 MaxIntValue = 0x7ffffff; const Int32 LabelLimit = 63; const Int32 DefaultNameLimit = 255; // 0 length strings aren't allowed if (text.Length == 0) { return(text); } var output = new StringBuilder(text.Length); var iNextDot = 0; var iAfterLastDot = 0; var iOutputAfterLastDot = 0; // Find the next dot while (iNextDot < text.Length) { // Find end of this segment iNextDot = text.IndexOfAny(possibleDots, iAfterLastDot); if (iNextDot < 0) { iNextDot = text.Length; } // Only allowed to have empty . section at end (www.microsoft.com.) if (iNextDot == iAfterLastDot) { break; } // We'll need an Ace prefix output.Append(acePrefix); var basicCount = 0; var numProcessed = 0; for (basicCount = iAfterLastDot; basicCount < iNextDot; basicCount++) { if (text[basicCount] < 0x80) { output.Append(EncodeBasic(text[basicCount])); numProcessed++; } else if (Char.IsSurrogatePair(text, basicCount)) { basicCount++; } } var numBasicCodePoints = numProcessed; if (numBasicCodePoints == iNextDot - iAfterLastDot) { output.Remove(iOutputAfterLastDot, acePrefix.Length); } else { // If it has some non-basic code points the input cannot start with xn-- if (text.Length - iAfterLastDot >= acePrefix.Length && text.Substring(iAfterLastDot, acePrefix.Length).Equals(acePrefix, StringComparison.OrdinalIgnoreCase)) { break; } // Need to do ACE encoding var numSurrogatePairs = 0; // Add a delimiter (-) if we had any basic code points (between basic and encoded pieces) if (numBasicCodePoints > 0) { output.Append(Text.Symbols.Minus); } // Initialize the state var n = InitialNumber; var delta = 0; var bias = InitialBias; // Main loop while (numProcessed < (iNextDot - iAfterLastDot)) { var j = 0; var m = 0; var test = 0; for (m = MaxIntValue, j = iAfterLastDot; j < iNextDot; j += IsSupplementary(test) ? 2 : 1) { test = Char.ConvertToUtf32(text, j); if (test >= n && test < m) { m = test; } } // Increase delta enough to advance the decoder's // <n,i> state to <m,0>, but guard against overflow: delta += (m - n) * ((numProcessed - numSurrogatePairs) + 1); n = m; for (j = iAfterLastDot; j < iNextDot; j += IsSupplementary(test) ? 2 : 1) { // Make sure we're aware of surrogates test = Char.ConvertToUtf32(text, j); // Adjust for character position (only the chars in our string already, some // haven't been processed. if (test < n) { delta++; } else if (test == n) { // Represent delta as a generalized variable-length integer: int q, k; for (q = delta, k = PunycodeBase; ; k += PunycodeBase) { var t = k <= bias ? Tmin : k >= bias + Tmax ? Tmax : k - bias; if (q < t) { break; } output.Append(EncodeDigit(t + (q - t) % (PunycodeBase - t))); q = (q - t) / (PunycodeBase - t); } output.Append(EncodeDigit(q)); bias = AdaptChar(delta, (numProcessed - numSurrogatePairs) + 1, numProcessed == numBasicCodePoints); delta = 0; numProcessed++; if (IsSupplementary(m)) { numProcessed++; numSurrogatePairs++; } } } ++delta; ++n; } } // Make sure its not too big if (output.Length - iOutputAfterLastDot > LabelLimit) { throw new ArgumentException(); } // Done with this segment, add dot if necessary if (iNextDot != text.Length) { output.Append(possibleDots[0]); } iAfterLastDot = iNextDot + 1; iOutputAfterLastDot = output.Length; } var rest = IsDot(text[text.Length - 1]) ? 0 : 1; var maxlength = DefaultNameLimit - rest; // Throw if we're too long if (output.Length > maxlength) { output.Remove(maxlength, output.Length - maxlength); } return(output.ToString()); }
//String[] rA={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; public String getGenID() { String len = "0000000000"; //r1.Next(Char.ConvertToUtf32("A",0), Char.ConvertToUtf32("Z",0)); len += r.Next(0, 999999999).ToString(); len = len.Substring(len.Length - 10); len = char.ConvertFromUtf32(r1.Next(Char.ConvertToUtf32("A", 0), Char.ConvertToUtf32("Z", 0))) + char.ConvertFromUtf32(r1.Next(Char.ConvertToUtf32("A", 0), Char.ConvertToUtf32("Z", 0))) + len; return(len); }
public virtual void Write(string value) { if (value == null) { this.Writer.Write(JsonReader.LiteralNull); return; } int start = 0, length = value.Length; this.Writer.Write(JsonReader.OperatorStringDelim); for (int i = start; i < length; i++) { char ch = value[i]; if (ch <= '\u001F' || ch >= '\u007F' || ch == '<' || // improves compatibility within script blocks ch == JsonReader.OperatorStringDelim || ch == JsonReader.OperatorCharEscape) { if (i > start) { this.Writer.Write(value.Substring(start, i - start)); } start = i + 1; switch (ch) { case JsonReader.OperatorStringDelim: case JsonReader.OperatorCharEscape: { this.Writer.Write(JsonReader.OperatorCharEscape); this.Writer.Write(ch); continue; } case '\b': { this.Writer.Write("\\b"); continue; } case '\f': { this.Writer.Write("\\f"); continue; } case '\n': { this.Writer.Write("\\n"); continue; } case '\r': { this.Writer.Write("\\r"); continue; } case '\t': { this.Writer.Write("\\t"); continue; } default: { this.Writer.Write("\\u"); this.Writer.Write(Char.ConvertToUtf32(value, i).ToString("X4")); continue; } } } } if (length > start) { this.Writer.Write(value.Substring(start, length - start)); } this.Writer.Write(JsonReader.OperatorStringDelim); }
public void TestConvertUtf32Fail2() { Char.ConvertToUtf32('\uD800', '\uD800'); }