microsoft/TypeAgent
Publicmirrored from https://github.com/microsoft/TypeAgentAvailable
dotnet/email/BodyParser.cs
50lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | namespace TypeAgent; |
| 5 | |
| 6 | public class BodyParser |
| 7 | { |
| 8 | public static readonly BodyParser Default = new BodyParser(); |
| 9 | |
| 10 | List<string> _delimiters; |
| 11 | public BodyParser() |
| 12 | { |
| 13 | _delimiters = new List<string> |
| 14 | { |
| 15 | "From:", |
| 16 | "Sent:", |
| 17 | "To:", |
| 18 | "Subject:", |
| 19 | "-----Original Message-----", |
| 20 | "----- Forwarded by", |
| 21 | "________________________________________" |
| 22 | }; |
| 23 | } |
| 24 | |
| 25 | public List<string> Delimiters => _delimiters; |
| 26 | |
| 27 | public string GetLatest(string body) |
| 28 | { |
| 29 | if (string.IsNullOrEmpty(body)) |
| 30 | { |
| 31 | return string.Empty; |
| 32 | } |
| 33 | int firstDelimiterAt = -1; |
| 34 | foreach (var delimiter in _delimiters) |
| 35 | { |
| 36 | int index = body.IndexOf(delimiter); |
| 37 | if (index >= 0 && (firstDelimiterAt == -1 || index < firstDelimiterAt)) |
| 38 | { |
| 39 | firstDelimiterAt = index; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | if (firstDelimiterAt >= 0) |
| 44 | { |
| 45 | return body[..firstDelimiterAt].Trim(); |
| 46 | } |
| 47 | |
| 48 | return body; |
| 49 | } |
| 50 | } |
| 51 | |