private static byte[] Prepare(byte[] src, StringPrep strprep) { String s = Encoding.UTF8.GetString(src); UCharacterIterator iter = UCharacterIterator.GetInstance(s); StringBuffer @out = strprep.Prepare(iter, StringPrepOptions.Default); return(Encoding.UTF8.GetBytes(@out.ToString())); }
public void TestPrepare() { StringPrep sp = StringPrep.GetInstance(0); try { if (!(sp.Prepare("dummy", 0)).Equals("dummy")) { Errln("StringPrep.prepare(String,int) was suppose to return " + "'dummy'"); } } catch (Exception e) { Errln("StringPrep.prepare(String,int) was not suppose to return " + "an exception."); } }
public void TestProfiles() { String profileName = null; StringPrep sprep = null; String result = null; String src = null; String expected = null; for (int i = 0; i < testCases.Length; i++) { for (int j = 0; j < testCases[i].Length; j++) { if (j == 0) { profileName = testCases[i][j]; sprep = StringPrep.GetInstance(GetOptionFromProfileName(profileName)); } else { src = testCases[i][j]; expected = testCases[i][++j]; try { result = sprep.Prepare(src, StringPrepOptions.AllowUnassigned); if (src.StartsWith("FAIL", StringComparison.Ordinal)) { Errln("Failed: Expected error for Test[" + i + "] Profile: " + profileName); } else if (!result.Equals(expected)) { Errln("Failed: Test[" + i + "] Result string does not match expected string for StringPrep test for profile: " + profileName); } } catch (StringPrepParseException ex) { if (!src.StartsWith("FAIL", StringComparison.Ordinal)) { Errln("Failed: Test[" + i + "] StringPrep profile " + profileName + " got error: " + ex); } } } } } }
public static StringBuffer ConvertToASCII(UCharacterIterator src, IDNA2003Options options) { bool[] caseFlags = null; // the source contains all ascii codepoints bool srcIsASCII = true; // assume the source contains all LDH codepoints bool srcIsLDH = true; //get the options bool useSTD3ASCIIRules = ((options & IDNA2003Options.UseSTD3Rules) != 0); int ch; // step 1 while ((ch = src.Next()) != UCharacterIterator.DONE) { if (ch > 0x7f) { srcIsASCII = false; } } int failPos = -1; src.SetToStart(); StringBuffer processOut = null; // step 2 is performed only if the source contains non ASCII if (!srcIsASCII) { // step 2 processOut = namePrep.Prepare(src, (StringPrepOptions)options); } else { processOut = new StringBuffer(src.GetText()); } int poLen = processOut.Length; if (poLen == 0) { throw new StringPrepParseException("Found zero length lable after NamePrep.", StringPrepErrorType.ZeroLengthLabel); } StringBuffer dest = new StringBuffer(); // reset the variable to verify if output of prepare is ASCII or not srcIsASCII = true; // step 3 & 4 for (int j = 0; j < poLen; j++) { ch = processOut[j]; if (ch > 0x7F) { srcIsASCII = false; } else if (IsLDHChar(ch) == false) { // here we do not assemble surrogates // since we know that LDH code points // are in the ASCII range only srcIsLDH = false; failPos = j; } } if (useSTD3ASCIIRules == true) { // verify 3a and 3b if (srcIsLDH == false || /* source contains some non-LDH characters */ processOut[0] == HYPHEN || processOut[processOut.Length - 1] == HYPHEN) { /* populate the parseError struct */ if (srcIsLDH == false) { throw new StringPrepParseException("The input does not conform to the STD 3 ASCII rules", StringPrepErrorType.STD3ASCIIRulesError, processOut.ToString(), (failPos > 0) ? (failPos - 1) : failPos); } else if (processOut[0] == HYPHEN) { throw new StringPrepParseException("The input does not conform to the STD 3 ASCII rules", StringPrepErrorType.STD3ASCIIRulesError, processOut.ToString(), 0); } else { throw new StringPrepParseException("The input does not conform to the STD 3 ASCII rules", StringPrepErrorType.STD3ASCIIRulesError, processOut.ToString(), (poLen > 0) ? poLen - 1 : poLen); } } } if (srcIsASCII) { dest = processOut; } else { // step 5 : verify the sequence does not begin with ACE prefix if (!StartsWithPrefix(processOut)) { //step 6: encode the sequence with punycode caseFlags = new bool[poLen]; StringBuilder punyout = Punycode.Encode(processOut, caseFlags); // convert all codepoints to lower case ASCII StringBuffer lowerOut = ToASCIILower(punyout); //Step 7: prepend the ACE prefix dest.Append(ACE_PREFIX, 0, ACE_PREFIX.Length - 0); // ICU4N: Checked 3rd parameter //Step 6: copy the contents in b2 into dest dest.Append(lowerOut); } else { throw new StringPrepParseException("The input does not start with the ACE Prefix.", StringPrepErrorType.AcePrefixError, processOut.ToString(), 0); } } if (dest.Length > MAX_LABEL_LENGTH) { throw new StringPrepParseException("The labels in the input are too long. Length > 63.", StringPrepErrorType.LabelTooLongError, dest.ToString(), 0); } return(dest); }