singrdk/base/Kernel/Singularity.Directory/SbUtils.sg

168 lines
4.9 KiB
Plaintext
Raw Normal View History

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