public virtual void TestEncodeEmpty() { IStringEncoder encoder = this.StringEncoder; encoder.Encode(""); encoder.Encode(" "); encoder.Encode("\t"); }
public virtual void TestLocaleIndependence() { IStringEncoder encoder = this.StringEncoder; string[] data = { "I", "i", }; CultureInfo orig = CultureInfo.CurrentCulture; CultureInfo[] locales = { new CultureInfo("en"), new CultureInfo("tr"), CultureInfo.CurrentCulture }; try { foreach (string element in data) { string @ref = null; for (int j = 0; j < locales.Length; j++) { //Locale.setDefault(locales[j]); #if NETSTANDARD CultureInfo.CurrentCulture = locales[j]; #else Thread.CurrentThread.CurrentCulture = locales[j]; #endif if (j <= 0) { @ref = encoder.Encode(element); } else { string cur = null; try { cur = encoder.Encode(element); } catch (Exception e) { Assert.Fail(CultureInfo.CurrentCulture.ToString() + ": " + e.Message); } Assert.AreEqual(@ref, cur, CultureInfo.CurrentCulture.ToString() + ": "); } } } } finally { //Locale.setDefault(orig); #if NETSTANDARD CultureInfo.CurrentCulture = orig; #else Thread.CurrentThread.CurrentCulture = orig; #endif } }
public override bool IncrementToken() { if (save != null) { // clearAttributes(); // not currently necessary RestoreState(save); save = null; return(true); } if (!m_input.IncrementToken()) { return(false); } // pass through zero-length terms if (termAtt.Length == 0) { return(true); } string value = termAtt.ToString(); string phonetic = null; try { string v = encoder.Encode(value); if (v.Length > 0 && !value.Equals(v, StringComparison.Ordinal)) { phonetic = v; } } catch (Exception) { /* ignored */ } // just use the direct text if (phonetic == null) { return(true); } if (!inject) { // just modify this token termAtt.SetEmpty().Append(phonetic); return(true); } // We need to return both the original and the phonetic tokens. // to avoid a orig=captureState() change_to_phonetic() saved=captureState() restoreState(orig) // we return the phonetic alternative first int origOffset = posAtt.PositionIncrement; posAtt.PositionIncrement = 0; save = CaptureState(); posAtt.PositionIncrement = origOffset; termAtt.SetEmpty().Append(phonetic); return(true); }
public string GenerateCacheKeyFromRequest(params string[] args) { if (args.Length == 0) { return(string.Empty); } var key = string.Join("_", args); key = _stringEncoder.Encode(key); return(key); }
public virtual void TestEncodeNull() { IStringEncoder encoder = this.StringEncoder; try { encoder.Encode(null); } #pragma warning disable 168 catch (/*Encoder*/ Exception ee) #pragma warning restore 168 { // An exception should be thrown } }
/// <summary> /// Formatea un campo cuyo valor es una cadena de caracteres. /// </summary> /// <param name="header"> /// Es el cabezal a formatear. /// </param> /// <param name="formatterContext"> /// Es el contexto de formateo que debe ser empleado. /// </param> public virtual void Format(MessageHeader header, ref FormatterContext formatterContext) { string headerValue = null; if (header != null) { if (!(header is StringMessageHeader)) { throw new ArgumentException(SR.StringHeaderExpected, "header"); } headerValue = (( StringMessageHeader)header).Value; } // Pad if padding available. if (_padding != null) { headerValue = _padding.Pad(headerValue, _lengthManager.MaximumLength); } if (headerValue == null) { _lengthManager.WriteLength(header, 0, 0, ref formatterContext); _lengthManager.WriteLengthTrailer(header, 0, 0, ref formatterContext); } else { _lengthManager.WriteLength(header, headerValue.Length, _encoder.GetEncodedLength(headerValue.Length), ref formatterContext); _encoder.Encode(headerValue, ref formatterContext); _lengthManager.WriteLengthTrailer(header, headerValue.Length, _encoder.GetEncodedLength(headerValue.Length), ref formatterContext); } }
/// <summary> /// Formatea un campo cuyo valor es una cadena de caracteres. /// </summary> /// <param name="field"> /// Es el campo a formatear. /// </param> /// <param name="formatterContext"> /// Es el contexto de formateo que debe ser empleado. /// </param> public override void Format(Field field, ref FormatterContext formatterContext) { if (!(field is StringField)) { throw new ArgumentException(SR.StringMessageFieldExpected, "field"); } string fieldValue = (( StringField)field).FieldValue; // Pad if padding available. if (_padding != null) { fieldValue = _padding.Pad(fieldValue, _lengthManager.MaximumLength); } if (_validator != null) { _validator.Validate(fieldValue); } if (fieldValue == null) { _lengthManager.WriteLength(field, 0, 0, ref formatterContext); _lengthManager.WriteLengthTrailer(field, 0, 0, ref formatterContext); } else { _lengthManager.WriteLength(field, fieldValue.Length, _encoder.GetEncodedLength(fieldValue.Length), ref formatterContext); _encoder.Encode(fieldValue, ref formatterContext); _lengthManager.WriteLengthTrailer(field, fieldValue.Length, _encoder.GetEncodedLength(fieldValue.Length), ref formatterContext); } }
/// <summary> /// Encodes the Strings and returns the number of characters in the two /// encoded Strings that are the same. /// <list type="bullet"> /// <item><description> /// For Soundex, this return value ranges from 0 through 4: 0 indicates /// little or no similarity, and 4 indicates strong similarity or identical /// values. /// </description></item> /// <item><description>For refined Soundex, the return value can be greater than 4.</description></item> /// </list> /// <para/> /// See: <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp"> /// MS T-SQL DIFFERENCE</a> /// </summary> /// <param name="encoder">The encoder to use to encode the strings.</param> /// <param name="s1">A string that will be encoded and compared.</param> /// <param name="s2">A string that will be encoded and compared.</param> /// <returns>The number of characters in the two Soundex encoded strings that are the same.</returns> /// <seealso cref="DifferenceEncoded(string, string)"/> public static int Difference(IStringEncoder encoder, string s1, string s2) { return(DifferenceEncoded(encoder.Encode(s1), encoder.Encode(s2))); }