本文整理汇总了C#中Slice.Len方法的典型用法代码示例。如果您正苦于以下问题:C# Slice.Len方法的具体用法?C# Slice.Len怎么用?C# Slice.Len使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Slice
的用法示例。
在下文中一共展示了Slice.Len方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestEmptySliceAtEnd
public void TestEmptySliceAtEnd()
{
Slice<char> x = new Slice<char>("test!");
x = x.Slc(x.Len());
Assert.AreEqual("", x.ToString());
}
示例2: TestGetHashCodeIsZeroForEmptySlice
public void TestGetHashCodeIsZeroForEmptySlice()
{
var s = new Slice<int>(new int[0]);
Assert.AreEqual(0, s.GetHashCode());
s = new Slice<int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
s = s.Slc(5, 5);
Assert.AreEqual(0, s.Len());
Assert.AreEqual(0, s.GetHashCode());
}
示例3: TestIndexOfSlice
public void TestIndexOfSlice()
{
var s = new Slice<int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
int i;
for (i = 0; i < s.Len(); i++)
{
Assert.AreEqual(i + 1, s[i]);
}
Assert.Throws<IndexOutOfRangeException>(() => { int a = s[i]; });
}
示例4: leadingInt
/// <summary>
/// leadingInt consumes the leading [0-9]* from s.
/// </summary>
private static long leadingInt(ref Slice<char> s)
{
int i = 0;
long x = 0;
for (; i < s.Len(); i++)
{
char c = s[i];
if (c < '0' || c > '9')
{
break;
}
if (x >= (long.MaxValue - 10) / 10)
{
// overflow
throw new OverflowException(s.ToString());
}
x = x * 10 + (c - '0');
}
s = s.Slc(i);
return x;
}
示例5: ParseDuration
/// <summary>
/// ParseDuration parses a duration string.
/// A duration string is a possibly signed sequence of
/// decimal numbers, each with optional fraction and a unit suffix,
/// such as "300ms", "-1.5h" or "2h45m".
/// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
/// </summary>
/// <param name="value">The value to parse.</param>
/// <returns>The parsed duration.</returns>
public static long ParseDuration(string value)
{
if (value == null)
throw new ArgumentNullException("value");
// [-+]?([0-9]*(\.[0-9]*)?[a-z]+)+
string orig = value;
long f = 0;
bool neg = false;
Slice<char> s = new Slice<char>(value);
// Consume [-+]?
if (s != "")
{
var c = s[0];
if (c == '-' || c == '+')
{
neg = (c == '-');
s = s.Slc(1);
}
}
// Special case: if all that is left is "0", this is zero.
if (s == "0")
{
return 0;
}
if (s == "")
{
throw new InvalidDataException("time: invalid duration " + orig);
}
while (s != "")
{
// The next character must be [0-9.]
if (!(s[0] == '.' || ('0' <= s[0] && s[0] <= '9')))
{
throw new InvalidDataException("time: invalid duration " + orig);
}
// Consume [0-9]*
var pl1 = s.Len();
long x = leadingInt(ref s);
double g = x;
bool pre = (pl1 != s.Len()); // whether we consumed anything before a period
// Consume (\.[0-9]*)?
bool post = false;
if (s != "" && s[0] == '.')
{
s = s.Slc(1);
int pl2 = s.Len();
x = leadingInt(ref s);
double scale = 1.0;
for (var n = pl2 - s.Len(); n > 0; n--)
{
scale *= 10;
}
g += x / scale;
post = (pl2 != s.Len());
}
if (!pre && !post)
{
// no digits (e.g. ".s" or "-.s")
throw new InvalidDataException("time: invalid duration " + orig);
}
// Consume unit.
int i = 0;
for (; i < s.Len(); i++)
{
char c = s[i];
if (c == '.' || ('0' <= c && c <= '9'))
{
break;
}
}
if (i == 0)
{
throw new InvalidDataException("time: missing unit in duration " + orig);
}
var u = s.Slc(0, i);
s = s.Slc(i);
double unit;
bool ok = _unitMap.TryGetValue(u.ToString(), out unit);
if (!ok)
{
throw new InvalidDataException("time: unknown unit " + u + " in duration " + orig);
//.........这里部分代码省略.........
示例6: TestSlcHasNoSideEffects
public void TestSlcHasNoSideEffects()
{
Slice<char> x = new Slice<char>("hello world");
x.Slc(2, 8);
Assert.AreEqual("hello world".Length, x.Len());
Assert.AreEqual("hello world", x.ToString());
}