singrdk/base/Applications/NameSpace/AtomicTestDSP/SbUtils.sg

155 lines
4.5 KiB
Plaintext
Raw Permalink Normal View History

2008-03-05 09:52:00 -05:00
////////////////////////////////////////////////////////////////////////////////
//
// Microsoft Research Singularity
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
2008-11-17 18:29:00 -05:00
// Note: StringBuilder Utility Class
2008-03-05 09:52:00 -05:00
//
using System;
using System.Text;
using Microsoft.Singularity.Channels;
#if !SINGULARITY_PROCESS
namespace Microsoft.Singularity.Directory
#else
using Microsoft.Singularity;
using Microsoft.Singularity.V1.Services;
namespace Microsoft.Application.DSP
#endif
{
public class SbUtils
{
private const char delimiter = '/';
public static bool IsTail(StringBuilder! sb)
{
if (sb.Length <= 1) return false;
int pos = IndexOf(sb,delimiter,1);
if (pos == -1) return true;
return false;
}
public static int IndexOf (StringBuilder! sb, char theChar, int startPos)
{
if (sb.Length == 0) return 0;
2008-11-17 18:29:00 -05:00
for (int i = startPos; i < sb.Length; i++) {
2008-03-05 09:52:00 -05:00
if (sb[i] == theChar) return i;
}
return -1;
}
public static int LastIndexOf (StringBuilder! sb, char theChar, int startPos)
{
if (sb.Length == 0) return 0;
2008-11-17 18:29:00 -05:00
for (int i = sb.Length; i > startPos + 1; i--) {
if (sb[i - 1] == theChar) return i;
2008-03-05 09:52:00 -05:00
}
return -1;
}
public static bool Empty (StringBuilder! sb)
{
2008-11-17 18:29:00 -05:00
if (sb.Length > 0) {
2008-03-05 09:52:00 -05:00
return true;
}
else {
return false;
}
}
public int Length(StringBuilder! sb)
{
return sb.Length;
}
public static StringBuilder! StripLast(StringBuilder! sb)
{
int pos = LastIndexOf(sb,delimiter,0);
2008-11-17 18:29:00 -05:00
if ((pos + 1) < sb.Length) {
2008-03-05 09:52:00 -05:00
sb.Length = pos-1;
}
else {
sb.Length = 0;
}
return sb;
}
public static StringBuilder! RemoveFirstElement(StringBuilder! sb)
{
return StripFirst(sb);
}
public static StringBuilder! StripFirst(StringBuilder! sb)
{
2008-11-17 18:29:00 -05:00
if (sb[0] == delimiter) {
2008-03-05 09:52:00 -05:00
int pos = IndexOf(sb, delimiter,1); // just in case it starts with '/'
2008-11-17 18:29:00 -05:00
if ((pos >= 0) && (pos < sb.Length)) {
2008-03-05 09:52:00 -05:00
sb.Remove(0,pos);
return sb;
}
sb.Length = 0;
return sb;
}
else {
int pos = IndexOf(sb,delimiter,0); // just in case it starts with '/'
2008-11-17 18:29:00 -05:00
if ((pos >= 0) && (pos < sb.Length)) {
2008-03-05 09:52:00 -05:00
sb.Remove(0,pos);
return sb;
}
sb.Length = 0;
return sb;
}
}
public static void Append(StringBuilder! sb, String! elt)
{
sb.Append(elt);
}
// Return a st
//
public static String FirstElement(StringBuilder! sb)
{
if (sb.Length == 0) return null;
2008-11-17 18:29:00 -05:00
if (sb[0] == delimiter) {
2008-03-05 09:52:00 -05:00
int pos = IndexOf(sb,delimiter,1);
// DebugStub.WriteLine(" FindFirst:pos={0},len={1},s={2}",__arglist(pos,sb.Length,sb.ToString()));
//DebugStub.Break();
if (pos == -1) {
2008-11-17 18:29:00 -05:00
if (sb.Length == 1) return null; // just the delimiter
2008-03-05 09:52:00 -05:00
return sb.ToString(1,sb.Length-1);
}
2008-11-17 18:29:00 -05:00
if ((pos > 1) && (pos < sb.Length)) {
2008-03-05 09:52:00 -05:00
return sb.ToString(1,pos-1);
}
return null;
}
else {
int pos = IndexOf(sb,delimiter,0);
if (pos == -1) {
return sb.ToString();
}
2008-11-17 18:29:00 -05:00
if ((pos < sb.Length) && (pos > 0)) {
2008-03-05 09:52:00 -05:00
return sb.ToString(0,pos);
}
return null;
}
}
public static String PathString(StringBuilder! sb)
{
2008-11-17 18:29:00 -05:00
if (sb.Length == 0) return null;
2008-03-05 09:52:00 -05:00
return sb.ToString();
}
public static char[]! in ExHeap PathVector(StringBuilder! sb)
{
return Bitter.FromString2(sb.ToString());
}
}
}