本文整理汇总了C#中ByteString.Substring方法的典型用法代码示例。如果您正苦于以下问题:C# ByteString.Substring方法的具体用法?C# ByteString.Substring怎么用?C# ByteString.Substring使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ByteString
的用法示例。
在下文中一共展示了ByteString.Substring方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FromUri
private static ImapSearchCriteria FromUri(Uri uri,
bool convertLiteral,
bool synchronizedLiteral,
bool splitCharset,
out bool containsLiteral,
out string charset)
{
if (uri == null)
throw new ArgumentNullException("uri");
containsLiteral = false;
charset = null;
var q = uri.Query;
if (q.Length == 0)
return null;
else if (q.Length == 1) // '?'
return new ImapSearchCriteria(string.Empty);
/*
* http://tools.ietf.org/html/rfc5092
* RFC 5092 - IMAP URL Scheme
*
* Note that quoted strings and non-synchronizing literals [LITERAL+]
* are allowed in the <enc-search> content; however, synchronizing
* literals are not allowed, as their presence would effectively mean
* that the agent interpreting IMAP URLs needs to parse an <enc-search>
* content, find all synchronizing literals, and perform proper command
* continuation request handling (see Sections 4.3 and 7 of [IMAP4]).
*/
var query = PercentEncoding.Decode(q.Substring(1), false);
var len = query.Length;
var convertedQuery = new ByteStringBuilder(len);
if (splitCharset) {
var queryString = new ByteString(query);
if (queryString.StartsWithIgnoreCase(charsetSpecification)) {
// CHARSET<SP>astring<SP>
var posEndOfCharset = queryString.IndexOf(Octets.SP, charsetSpecification.Length);
if (posEndOfCharset < 0) {
throw new ArgumentException("search criteria contains invalid charset specification", "uri");
}
else {
charset = queryString.Substring(charsetSpecification.Length,
posEndOfCharset - charsetSpecification.Length).ToString();
query = queryString.Substring(posEndOfCharset + 1).ByteArray;
len = query.Length;
}
}
}
for (var i = 0; i < len;) {
if (query[i] == ImapOctets.DQuote) {
/*
* quoted
*/
var start = i;
for (;;) {
if (++i == len)
throw new ArgumentException("search criteria contains unclosed quoted string", "uri");
if (query[i] == ImapOctets.DQuote) {
break;
}
else if (query[i] == ImapOctets.BackSlash) {
if (++i == len || !(query[i] == ImapOctets.DQuote || query[i] == ImapOctets.BackSlash))
throw new ArgumentException("search criteria contains invalid quoted string", "uri");
}
}
i++;
convertedQuery.Append(query, start, i - start);
}
else if (query[i] == ImapOctets.OpenBrace) {
/*
* literal
*/
var start = i;
var isLiteralSynchronizing = false;
var literalLength = 0;
for (;;) {
if (++i == len)
throw new ArgumentException("search criteria contains incomplete literal", "uri");
if (Octets.IsDecimalNumber(query[i])) {
literalLength = literalLength * 10 + (query[i] - 0x30 /* '0' */);
// TODO: check length
}
else if (query[i] == ImapOctets.CloseBrace) {
// {xxx}
isLiteralSynchronizing = true;
break;
}
//.........这里部分代码省略.........
示例2: EncodeCriteria
private byte[] EncodeCriteria(ImapSearchCriteria criteria)
{
Enqueue(new[] {criteria});
Enqueue(Octets.CRLF);
Send();
try {
var innerStream = Stream.InnerStream as MemoryStream;
innerStream.Close();
var ret = new ByteString(innerStream.ToArray());
return ret.Substring(0, ret.Length - 2 /*CRLF*/).ByteArray;
}
finally {
Stream.Close();
}
}
示例3: ParseText
private PopResponseText ParseText(byte[] line, int posStatusEnd)
{
/*
* http://tools.ietf.org/html/rfc2449
* 3. General Command and Response Grammar
* single-line = status [SP text] CRLF ;512 octets maximum
* status = "+OK" / "-ERR"
* text = *schar / resp-code *CHAR
* resp-code = "[" resp-level *("/" resp-level) "]"
* resp-level = 1*rchar
* schar = %x21-5A / %x5C-7F
* ;printable ASCII, excluding "["
* rchar = %x21-2E / %x30-5C / %x5E-7F
* ;printable ASCII, excluding "/" and "]"
*/
if (line.Length - 2 == posStatusEnd)
return new PopResponseText();
posStatusEnd++; // SP
var text = new ByteString(line, posStatusEnd, line.Length - (posStatusEnd + 2/*CRLF*/));
if (text[0] != PopOctets.OpenBracket)
return new PopResponseText(null, text);
var respCodeEnd = text.IndexOf(PopOctets.CloseBracket);
if (respCodeEnd < 0)
return new PopResponseText(null, text);
else
return new PopResponseText(PopResponseCode.GetKnownOrCreate(text.Substring(1/*'['*/, respCodeEnd - 1).ToString()),
text.Substring(respCodeEnd + 1).TrimStart());
}
示例4: ParseHeader
private static MimeHeaderCollection ParseHeader(LineOrientedStream stream)
{
var headers = new MimeHeaderCollection();
MimeHeader current = null;
for (;;) {
var lineBytes = stream.ReadLine(false);
if (lineBytes == null)
break; // unexpected end of stream
var line = new ByteString(lineBytes);
if (line.IsEmpty)
break; // end of headers
if (line[0] == Octets.HT || line[0] == Octets.SP) { // LWSP-char
// folding
if (current == null)
// ignore incorrect formed header
continue;
current.Value += Chars.SP;
current.Value += line.TrimStart().ToString();
}
else {
// field = field-name ":" [ field-body ] CRLF
// field-name = 1*<any CHAR, excluding CTLs, SPACE, and ":">
var delim = line.IndexOf(MimeHeader.NameBodyDelimiter); // ':'
if (delim < 0) {
// ignore incorrect formed header
current = null;
continue;
}
var header = new MimeHeader(line.Substring(0, delim).TrimEnd().ToString(),
line.Substring(delim + 1).TrimStart().ToString());
headers.Add(header);
current = header;
}
}
return headers;
}
示例5: ParseRespText
private ImapResponseText ParseRespText(ByteString respText)
{
// resp-text = ["[" resp-text-code "]" SP] text
if (respText[0] != ImapOctets.OpenBracket)
// no resp-text-code
return new ImapResponseText(DecodeRespTextString(respText.Substring(0, respText.Length - 2))); // remove trailing CRLF
// exists resp-text-code
var respTextCodeEnd = respText.IndexOf(ImapOctets.CloseBracket);
var respTextCode = respText.Substring(1, respTextCodeEnd - 1);
var codeSep = respTextCode.IndexOf(Octets.SP);
ByteString text;
if (respTextCodeEnd < respText.Length) {
text = respText.Substring(respTextCodeEnd + 1);
text = text.Trim(); // remove SP, CRLF
}
else {
text = ByteString.CreateEmpty();
}
if (codeSep == -1)
// no arguments; "READ-ONLY" / "READ-WRITE" / etc...
return new ImapResponseText(ImapResponseCode.GetKnownOrCreate(respTextCode.ToString()),
new ImapData[] {},
DecodeRespTextString(text));
// exist arguments; "UIDNEXT" SP nz-number / "UIDVALIDITY" SP nz-number / etc...
var codeString = respTextCode.Substring(0, codeSep);
var arguments = ParseDataNonTerminatedText(respTextCode, codeSep + 1);
if (arguments == null)
throw new ImapMalformedResponseException("malformed resp-text-code", respTextCode.ToString(codeSep + 1));
return new ImapResponseText(ImapResponseCode.GetKnownOrCreate(codeString.ToString(), arguments[0]),
arguments,
DecodeRespTextString(text));
}
示例6: ParseResponce
private ImapResponse ParseResponce(ByteString line, ref IParsingContext parsingContext)
{
// response = *(continue-req / response-data) response-done
// continue-req = "+" SP (resp-text / base64) CRLF
if (line.StartsWith(continueReqMark))
return new ImapCommandContinuationRequest(line.ToString(2, line.Length - 4)); // remove leading "+" SP and trailing CRLF
// greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF
// response-done = response-tagged / response-fatal
// response-data = "*" SP (resp-cond-state / resp-cond-bye /
// mailbox-data / message-data / capability-data) CRLF
// response-fatal = "*" SP resp-cond-bye CRLF
// ; Server closes connection immediately
// response-tagged = tag SP resp-cond-state CRLF
// ("*" / tag) SP
var tagSep = line.IndexOf(Octets.SP);
if (tagSep == -1)
// response-done and response-data must contain SP
throw new ImapMalformedResponseException("malformed response-done/response-data", line.ToString());
var untagged = (tagSep == 1 && line[0] == ImapOctets.Asterisk);
// ("OK" / "BAD" / "NO" / "BYE" / "PREAUTH" / text) SP
var respCondSep = line.IndexOf(Octets.SP, tagSep + 1);
var cond = ImapResponseCondition.Undefined;
if (respCondSep == -1) {
if (!untagged)
throw new ImapMalformedResponseException("malformed response-data", line.ToString());
//else
// '* SEARCH\r\n' (mailbox-data which contains no SP)
}
else {
cond = ParseCondition(line.Substring(tagSep + 1, respCondSep - tagSep - 1));
}
if (cond != ImapResponseCondition.Undefined || line.StartsWith(respCondMark)) {
// resp-cond-auth / resp-cond-state / resp-cond-bye
var responseText = ParseRespText((cond == ImapResponseCondition.Undefined)
? line.Substring(tagSep + 1)
: line.Substring(respCondSep + 1));
if (untagged)
return new ImapUntaggedStatusResponse((ImapResponseCondition)cond,
responseText);
else
return new ImapTaggedStatusResponse(line.ToString(0, tagSep),
(ImapResponseCondition)cond,
responseText);
}
// mailbox-data / message-data / capability-data etc.
return ParseDataResponse(line, ref parsingContext);
}
示例7: ParseChallenge
private static Dictionary<string, ByteString> ParseChallenge(ByteString str)
{
// quoted-string = ( <"> qdstr-val <"> )
// qdstr-val = *( qdtext | quoted-pair )
// qdtext = <any TEXT except <">>
// quoted-pair = "\" CHAR
var splitAt = new List<int>();
var quoted = false;
for (var i = 0; i < str.Length; i++) {
if (str[i] == '\\') // escape
i++;
else if (str[i] == '\"')
quoted = !quoted;
else if (!quoted && str[i] == ',')
splitAt.Add(i);
}
splitAt.Add(str.Length); // end of line
var pairs = new ByteString[splitAt.Count];
for (var i = 0; i < pairs.Length; i++) {
if (i == 0)
pairs[i] = str.Substring(0, splitAt[i]).Trim();
else
pairs[i] = str.Substring(splitAt[i - 1] + 1, splitAt[i] - splitAt[i - 1] - 1).Trim();
}
var ret = new Dictionary<string, ByteString>(StringComparer.Ordinal);
foreach (var p in Array.ConvertAll(pairs, delegate(ByteString pair) {
var delim = pair.IndexOf('=');
var dequote = (pair[delim + 1] == '\"') && pair.EndsWith("\"");
if (dequote)
return new KeyValuePair<string, ByteString>(pair.Substring(0, delim).ToString(),
pair.Substring(delim + 2, pair.Length - (delim + 3)));
else
return new KeyValuePair<string, ByteString>(pair.Substring(0, delim).ToString(),
pair.Substring(delim + 1));
})) {
// TODO: quoted-pair
ret.Add(p.Key, p.Value);
}
return ret;
}
示例8: TestSubstring
public void TestSubstring()
{
var str = new ByteString("abcde");
Assert.AreEqual(new ByteString("abcde"), str.Substring(0));
Assert.AreEqual(new ByteString("abcde"), str.Substring(0, 5));
Assert.AreEqual(new ByteString("cde"), str.Substring(2));
Assert.AreEqual(new ByteString("cd"), str.Substring(2, 2));
}
示例9: TestIndexOf
public void TestIndexOf()
{
var str = new ByteString("ababdabdbdabcab");
Assert.AreEqual(10, str.IndexOf(new ByteString("abc")));
Assert.AreEqual("abc", str.Substring(10, 3).ToString());
Assert.AreEqual(2, str.IndexOf(new ByteString("abd")));
Assert.AreEqual("abd", str.Substring(2, 3).ToString());
Assert.AreEqual(-1, str.IndexOf(new ByteString("abe")));
var substr = new ByteString("ab");
Assert.AreEqual(0, str.IndexOf(substr));
Assert.AreEqual(0, str.IndexOf(substr, 0));
Assert.AreEqual(2, str.IndexOf(substr, 1));
Assert.AreEqual(2, str.IndexOf(substr, 2));
Assert.AreEqual(5, str.IndexOf(substr, 3));
Assert.AreEqual(5, str.IndexOf(substr, 4));
Assert.AreEqual(5, str.IndexOf(substr, 5));
}