///// <summary>
        ///// Returns a boolean indicating whether digits are present in the string.
        ///// </summary>
        //static public bool ContainsDigits(String astring)
        //{
        //	int i = 0;
        //	if (IsBlank(astring)) return false;
        //	for (i = 0; i < astring.Length; i++)
        //	{
        //		if (char.IsDigit(astring, i)) return true;
        //	}
        //	return false;
        //}

        /// <summary>
        /// Returns a boolean indicating whether the first and last characters in the string
        /// are double quotation characters. No consideration of internal characters is given.
        /// </summary>
        static public bool IsQuoted(String astring)
        {
            if (ResourceStringLibrary.IsBlank(astring))
            {
                return(false);
            }
            return(((astring.IndexOf('"') == 0) && (astring.LastIndexOf('"')
                                                    == (astring.Length - 1))) ? true : false);
        }
示例#2
0
 /// <summary>
 /// Instantiate passed type from string, supplying legal default
 /// values when the string is unparsable.
 /// </summary>
 static public Object NewFromStringDefault(Type atype, String astring)
 {
     if (IsNumeric(atype) && ResourceStringLibrary.IsBlank(astring))
     {
         return(DBNull.Value);
     }
     //TODO 1: add other type tests
     return(NewFromString(atype, astring));
 }
        /// <summary>
        /// Returns a boolean indicating whether the string represents a whole number.
        /// The provided sign is used in order to recognize negative numbers.
        /// </summary>
        static public bool IsWholeNumber(String astring, NumberFormatInfo numberFormat)
        {
            int i      = 0;
            int acount = 0;

            if (ResourceStringLibrary.IsBlank(astring))
            {
                return(false);
            }
            for (i = 0; i < astring.Length; i++)
            {
                if (char.IsDigit(astring, i))
                {
                    acount++;
                }
            }

            if (acount == astring.Length)
            {
                return(true);
            }
            else
            {
                bool retVal;
                int  negativePattern = numberFormat.NumberNegativePattern;
                switch (negativePattern)
                {
                case 0:     // (n)
                    retVal = (astring.Length == acount + 2 && astring[0] == '(' && astring[astring.Length - 1] == ')');
                    break;

                case 1:     // -n
                    retVal = (astring.Length == acount + 1 && astring.Substring(0, numberFormat.NegativeSign.Length) == numberFormat.NegativeSign);
                    break;

                case 2:     // - n
                    retVal = (astring.Length == acount + 2 && astring.Substring(0, numberFormat.NegativeSign.Length) == numberFormat.NegativeSign);
                    break;

                case 3:     // n-
                    retVal = (astring.Length == acount + 1 &&
                              astring.Substring(astring.Length - numberFormat.NegativeSign.Length, numberFormat.NegativeSign.Length) == numberFormat.NegativeSign);
                    break;

                case 4:     // n -
                    retVal = (astring.Length == acount + 2 &&
                              astring.Substring(astring.Length - numberFormat.NegativeSign.Length, numberFormat.NegativeSign.Length) == numberFormat.NegativeSign);
                    break;

                default:
                    throw new IndexOutOfRangeException();
                }

                return(retVal);
            }
        }
        //TODO 3: consider using String.Format() for this?
        /// <summary>
        /// Plug the astringTemplate with the parms provided. Plug locations
        /// in the astringTemplate are marked by %n, where n is a single digit,
        /// equal to the index in argstring that should be used to plug the
        /// astringTemplate. If there are not enough parms, substitute a default
        /// string. Extended to support meta-chars for embedded carriage
        /// returns and tabs.
        /// </summary>
        public static String MessageFromParms(String astringTemplate, String[] argstring)
        {
            String astringDefault    = "??";
            String astringMessage    = "";
            int    aintIndexLast     = 0;
            int    aintIndex         = 0;
            int    aintNumberOfParms = argstring.Length;
            int    aintSize          = astringTemplate.Length;

            // Note: <whar> I profiled this against using a StringBuilder (in lieu of +=) and whilst StringBuilder is
            // marginally faster the amount by which it is faster did not justify making any changes. This code as-is, is
            // significantly faster than an approach using String.Replace.

            while (aintIndex < aintSize)
            {
                if (astringTemplate[aintIndex] == '%')
                {
                    char acharNext = astringTemplate[aintIndex + 1];
                    astringMessage = astringMessage + astringTemplate.Substring(aintIndexLast, aintIndex - aintIndexLast);

                    if (Char.IsDigit(acharNext))
                    {
                        int aintIndexParm = ResourceStringLibrary.DigitValue(acharNext) - 1;
                        astringMessage = astringMessage +
                                         (aintIndexParm < aintNumberOfParms ?
                                          argstring[aintIndexParm] : astringDefault);
                    }
                    else if (acharNext == 'n')
                    {
                        astringMessage += "\n";
                    }
                    else if (acharNext == 'r')
                    {
                        astringMessage += "\r";
                    }
                    else if (acharNext == 't')
                    {
                        astringMessage += "\t";
                    }
                    else
                    {
                        astringMessage += acharNext.ToString(CultureInfo.CurrentCulture);
                    }

                    //Skip over the %n in the string.
                    aintIndex    += 2;
                    aintIndexLast = aintIndex;
                }
                else
                {
                    aintIndex++;
                }
            }
            return(astringMessage + astringTemplate.Substring(aintIndexLast));
        }
        //      static public string GroupFormat(int number)
        //      {
        //          return string.Format("{0:n0}", number);
        //      }

        ////Answer astring in aa, ab, ac ... aaa, aab format
        //static public string AAFormat(int anumber)
        //{
        //	// HMGeoNetworkExternalDatabaseType uses this guy for ModelBuilder. It uses this to convert an integer
        //	// to it's equivalent string value... Talked to sbug and a simple integer.toString() would have fixed this instead.
        //	// He is looked at it for the ModelBuilder work.
        //	int avalue = anumber;
        //	int arem;
        //	StringBuilder abuilder = new StringBuilder();
        //	while(avalue > 0)
        //	{
        //		arem = ((avalue - 1) % 26) + 1;
        //		avalue = (avalue - arem) / 26;
        //		abuilder.Insert(0, (char)(arem - 1 + 'a'));
        //	}
        //	return(abuilder.ToString());
        //}

        //static public String AppendPathSeparatorIfNeeded(String astring)
        //{
        //	StringBuilder asb = new StringBuilder(astring);
        //	StringBuilderLibrary.AppendPathSeparatorIfNeeded(asb);
        //	return asb.ToString();
        //}

        /// <summary>
        /// Return the passed file extension in a standard format for comparison.
        /// Assumes passed string is an extension, not a full path.
        /// </summary>
        static public String CanonicalExtension(String astringExtension)
        {
            if (ResourceStringLibrary.IsBlank(astringExtension))
            {
                return(astringExtension);
            }
            if (astringExtension[0] != '.')                // I18n ok <abar>
            {
                astringExtension = '.' + astringExtension; // I18n ok <abar>
            }
            return(astringExtension.ToLowerInvariant());
        }
 /// <summary>
 /// Returns a boolean indicating whether any of the specified charcters are present in the string.
 /// </summary>
 static public bool Contains(String astring, String acharacters)
 {
     if (ResourceStringLibrary.IsBlank(astring))
     {
         return(false);
     }
     char[] acharArray = acharacters.ToCharArray();
     if ((astring.IndexOfAny(acharArray)) == -1)
     {
         return(false);
     }
     return(true);
 }
        /// <summary>
        /// Construct formatter using full state.
        /// </summary>
        public NumericFormatter(int aintId, String asName, Unit aunit, String asFormatCode, int aintDecimalDigits,
                                Unit aunitDisplayDefaultSi, Unit aunitDisplayDefaultUs, bool aboolIsStandardFormatter, String astringLabel)
        {
            m_intId                   = aintId;
            m_stringName              = asName;
            m_unitDisplay             = aunit;
            m_stringFormatCode        = asFormatCode;
            m_intDecimalDigits        = aintDecimalDigits;
            m_unitDisplayDefaultSi    = aunitDisplayDefaultSi;
            m_unitDisplayDefaultUs    = aunitDisplayDefaultUs;
            m_boolIsStandardFormatter = aboolIsStandardFormatter;
            m_stringLabel             = astringLabel;

            Debug.Assert(!ResourceStringLibrary.IsBlank(m_stringName));
            Debug.Assert(!ResourceStringLibrary.IsBlank(m_stringFormatCode));
            Debug.Assert(m_unitDisplay != null);
            Debug.Assert(m_unitDisplayDefaultSi == null || m_unitDisplayDefaultSi.Dimension == m_unitDisplay.Dimension);
            Debug.Assert(m_unitDisplayDefaultUs == null || m_unitDisplayDefaultUs.Dimension == m_unitDisplay.Dimension);
        }