INCLUDE_DATA

 
»
S
I
D
E
B
A
R
«
aquisto cialis levitra svizzera tadalafil bestellen viagra prix acheter du viagra acquista levitra cialis sur internet citrate de sildenafil compro viagra levitra ricetta kamagra pharmaceuticals cialis vente libre cialis suisse levitra italia vente de cialis viagra suisse kamagra gel viagra quanto costa cialis 20 mg prix cialis 10mg viagra verkauf cialis moins cher viagra prezzo levitra kopen acheter tadalafil acheter kamagra tadalafil generico impuissance sexuelle cialis preco levitra donna viagra 100 mg comprar cialis generico comprar levitra cialis a vendre procurer du cialis generische cialis vardenafil generico vardenafil generika levitra sur internet generische viagra acheter cialis generique acquista viagra achat cialis viagra 100 mg cialis generico kamagra en france viagra ordonnance acheter viagra achat vardenafil pastilla levitra viagra cialis differenze impotenza sessuale venta viagra medicament cialis curare impotenza kamagra te koop achat cialis 20mg levitra pharmacie cialis receta acquisto viagra net cialis en ligne achat de viagra cialis generique acheter acheter du cialis cialis 20 mg vardenafil 10 mg viagra alternativo citrate de sildenafil cialis sin receta viagra kopen acheter cialis en pharmacie kamagra bestellen comprar viagra pela internet viagra prescrizione levitra donne vente cialis venta de sildenafil achete levitra acheter cialis france venta de levitra viagra kosten cialis marche pas comprar vardenafil disfunzione erettile rimedi vardenafil generique viagra recensioni cialis generico prezzo viagra versand cialis europe viagra venta libre impotenza rimedi cialis rezeptfrei acheter kamagra france levitra ohne rezept acquisto viagra in contrassegno prix de cialis cialis prescrizione viagra acquisto online achat viagra pildoras cialis kamagra generique cialis prezzo cialis inde cialis sur le net acheter cialis en espagne levitra ordonnance viagra naturel cialis 10 mg acquistare levitra procurer du viagra acquisto viagra senza ricetta viagra controindicazioni levitra 20 mg compra viagra impuissance erection acheter cialis pharmacie prezzi levitra viagra ohne rezept kamagra apcalis comprar viagra commander du cialis cialis ricetta medica sildenafil 50 mg sildenafil venta viagra italia pilule levitra sildenafil generico viagra prijs
StatenUtil: C# String-Regex Extenstions and String Scanner
Apr 3rd, 2009 by Jason

Face it, C# is not as regex-friendly as it could be.  To do things like find a single regex match involves several lines or some nasty inline code. Using Extension methods, this can be simplified. In another addition to StatenUtil focused on regex, C# becomes a more coder-friendly language.

StringRegex.cs

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace StatenUtil.RegexExtensions
{
  public static class StringRegex
  {
    public static string RegexReplace(this string source, string regex, string replacement)
    {
      Regex r = new Regex(regex);
      return r.Replace(source, replacement);
    }
    public static string Match(this string source, string regex)
    {
      Regex r = new Regex(regex);
      Match m = r.Match(source);
      return m.Value;
    }
    public static string[] Matches(this string source, string regex)
    {
      Regex r = new Regex(regex);
      MatchCollection mc = r.Matches(source);
      List<string> matches = new List<string>();
      foreach(object o in mc)
        matches.Add(((Match)o).Value);
      return matches.ToArray();
    }
    public static string RegexRemove(this string source, string regex)
    {
      Regex r = new Regex(regex);
      return r.Replace(source, "");
    }
  }
}

This class offers four extension methods to the string class: Replace, Remove, Match first, and Match all. Aside from RegexReplace, the parameters often call for a single regular expression string.

Another addition that C# could use is a string scanner. A string scanner moves through a string one regex at a time matching the beginning of a string. This is useful for parsing languages in the way similar to JSON Parsing in Ruby.

StringScanner.cs

using System;

namespace StatenUtil.RegexExtensions
{
  public class StringScanner
  {
    public string Remaining { get; set; }
    public string Match { get; set; }
    public StringScanner(string input)
    {
      Remaining = input;
    }
    public string Scan(string regex)
    {
      Match = Remaining.Match("^"+regex);
      if (Match != null)
        Remaining = Remaining.Substring(Match.Length);
      return Match;
    }
  }
}

The StringScanner class constructor takes an input string that it will scan. Having only a Scan method, the class has the single use of moving through the input string as regex matches are made. As Scan finds a match, it returns the matching string. Also, the class has two properties: Remaining and Match. Remaining provides what is left in the string, useful for checking if the end of the input string has been met. Match provides the most recent match of the Scan method.

StatenUtil: Bringing a bit of Ruby looping into C#
Feb 28th, 2009 by Jason

For anyone who’s new to the blog, StatenUtil is a personal C# utility library. Consisting mainly of extension methods, its main objective is to simplify frequently used operations. This edition includes adding some Ruby-like looping methods into C#. I understand that Func and Action objects aren’t a real replacement for blocks. However, it’s nice to have some features I miss when not using Ruby.

Int32Extensions.cs

using System;

namespace StatenUtil
{
    public static class Int32Extensions
    {
        public static void Times(this int source, Action<int> action)
        {
            for (int i = 0; i < source; i++)
            {
                action(i);
            }
        }
        public static void UpTo(this int source, int max, Action<int> action)
        {
            for (int i = source; i <= max; i++)
            {
                action(i);
            }
        }
        public static void DownTo(this int source, int min, Action<int> action)
        {
            for (int i = source; i >= min; i--)
            {
                action(i);
            }
        }
        public static int[] Range(this int source, int max)
        {
            int difference = max - source;
            if (difference < 0) return new int[0];
            int[] ret = new int[difference + 1];
            for (int i = 0; i <= difference; i++)
                ret[i] = source+i;
            return ret;
        }

    }
}

This class file revolves around adding functions to the int class. The Times method allows for a specific integer capturing lambda expression to be executed source times. For example, 6.Times(x => Console.WriteLine(x)) would print lines from zero to 6.

UpTo and DownTo are similar to the Times method. However, they loop over a range of integers rather than from zero. Compare these to Ruby’s upto and downto methods.

Range is simply a way to create an array covering a range of integers. The source is the minimum value and max is the largest of the array.

IEnumerableExtensions.cs

using System;
using System.Collections.Generic;

namespace StatenUtil
{
    public static class IEnumerableExtensions
    {
        public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
        {
            IEnumerator<T> enumerator = source.GetEnumerator();
            while (enumerator.MoveNext())
                action(enumerator.Current);
        }
    }
}

Currently, this class file add inline ForEach capabilities to any generic IEnumerable object, similar to Ruby’s each. Although List has this capability, other IEnumerables like arrays do not.

That’s all for now. However, StatenUtil is a work in progress that’s always being amended. In each significant addition, the details will be shared once again.

StatenUtil: Adding common C# functionality using extension methods
Feb 2nd, 2009 by Jason

Working through endless “Wrestles” in CS290, I’ve come to find some common needs for many of my assignments. Because of this, I’ve started to create my own utility DLL (dynamic link library) of functions. I’ll be honest that not all of the ideas were my own, especially Jamie King’s P() method.

Extension Methods

For those who have not heard of or used extension methods, here’s a brief overview. Extension methods are simply static methods that are syntactic sugar.  To create an extension method, create a static class and within it create a static method having this before your first argument.

public static class MyStaticClass
{
public static void MyExtensionMethod(this Object source)
{
}
}

After creating this, you can then use it by calling it on any instance that inherits from Object (anything) as instanceName.MyExtensionMethod(). It’s the same as calling, MyStaticClass.MyExtensionMethod(instanceName), just shorter to type and implicitly inserting the parameter, making it feel like a method of the object, rather than a static extension.

Asserter.cs

Assertion is an effective way of debugging your code. When “wrestling” with new concepts, its a great way to prove things without spewing out a bunch of console.writeline(). These extension methods give you simple Assert methods to call on any object.

using System;
using System.Diagnostics;

namespace StatenUtil
{
public static class Asserter
{
public static void AssertReferenceEquals(this Object source, Object target)
{
Debug.Assert(Debug.ReferenceEquals(source, target), "Source and target references not equal");
}
public static void AssertEquals(this Object source, Object target)
{
if (source == null)
{
Debug.Assert(target == null, "Source is null, target is not");
}
else
{
Debug.Assert(source.Equals(target), "Objects are not equal");
}
}
public static void AssertNull(this Object source)
{
Debug.Assert(source == null, "Object is not null");
}

}
}

PrintExtensions.cs

Another frequet debugging technique is to print out to the console. Adding the P() extension method can simplify the need to write Console.Writeline(). Also, added is the iteration of enumerable objects to eliminate the need to foreach over its members.


using System;
using System.Collections;

namespace StatenUtil
{
public static class PrintExtensions
{
public static void P(this Object source)
{
P(source, "{0}");
}
public static void P(this Object source, string formatString)
{
if (!(source is String) && source is IEnumerable)
{
Console.WriteLine(formatString, source);
foreach (object o in (IEnumerable)source)
o.P(" " + formatString);
return;
}
Console.WriteLine(formatString, source);
}

}

}

StatenUtil: The Future

In the future, I’ll continue to add additional features to this utility set and share them as the changes become significant. Check back often, as StatenUtil has plenty of life ahead of it. If you have comments or suggestions, please let me know.

»  Substance: WordPress   »  Style: Ahren Ahimsa Blog Flux Local - Utah
© Jason Staten 2009