| 1 | #Region "Microsoft.VisualBasic::340c6fab2002017c3ece903b4f449d2d, Microsoft.VisualBasic.Core\Language\StringHelpers.vb" |
| 2 | |
| 3 | ' Author: |
| 4 | ' |
| 5 | ' asuka (amethyst.asuka@gcmodeller.org) |
| 6 | ' xie (genetics@smrucc.org) |
| 7 | ' xieguigang (xie.guigang@live.com) |
| 8 | ' |
| 9 | ' Copyright (c) 2018 GPL3 Licensed |
| 10 | ' |
| 11 | ' |
| 12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
| 13 | ' |
| 14 | ' |
| 15 | ' This program is free software: you can redistribute it and/or modify |
| 16 | ' it under the terms of the GNU General Public License as published by |
| 17 | ' the Free Software Foundation, either version 3 of the License, or |
| 18 | ' (at your option) any later version. |
| 19 | ' |
| 20 | ' This program is distributed in the hope that it will be useful, |
| 21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | ' GNU General Public License for more details. |
| 24 | ' |
| 25 | ' You should have received a copy of the GNU General Public License |
| 26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | |
| 28 | |
| 29 | |
| 30 | ' /********************************************************************************/ |
| 31 | |
| 32 | ' Summaries: |
| 33 | |
| 34 | ' Module FormatHelpers |
| 35 | ' |
| 36 | ' Function: StringFormat, xFormat |
| 37 | ' |
| 38 | ' Structure FormatHelper |
| 39 | ' |
| 40 | ' Function: ToString |
| 41 | ' Operators: (+2 Overloads) <=, (+2 Overloads) >= |
| 42 | ' |
| 43 | ' |
| 44 | ' /********************************************************************************/ |
| 45 | |
| 46 | #End Region |
| 47 | |
| 48 | Imports System.Runtime.CompilerServices |
| 49 | Imports Microsoft.VisualBasic.Language.C |
| 50 | |
| 51 | Namespace Language |
| 52 | |
| 53 | ''' <summary> |
| 54 | ''' ``<see cref="sprintf"/>`` syntax helpers |
| 55 | ''' </summary> |
| 56 | Public Module FormatHelpers |
| 57 | |
| 58 | ''' <summary> |
| 59 | ''' ``<see cref="sprintf"/>`` extensions |
| 60 | ''' </summary> |
| 61 | ''' <param name="s"></param> |
| 62 | ''' <returns></returns> |
| 63 | <Extension> |
| 64 | Public Function xFormat(s As String) As FormatHelper |
| 65 | Return New FormatHelper With {.source = s} |
| 66 | End Function |
| 67 | |
| 68 | ''' <summary> |
| 69 | ''' Synax like ``"formats" <= {args}.xFormat`` |
| 70 | ''' Format by <see cref="sprintf"/> |
| 71 | ''' </summary> |
| 72 | ''' <param name="args"></param> |
| 73 | ''' <returns></returns> |
| 74 | <Extension> |
| 75 | Public Function StringFormat(args As String()) As FormatHelper |
| 76 | Return New FormatHelper With {.args = args} |
| 77 | End Function |
| 78 | End Module |
| 79 | |
| 80 | ''' <summary> |
| 81 | ''' ``<see cref="sprintf"/>`` reference |
| 82 | ''' </summary> |
| 83 | Public Structure FormatHelper |
| 84 | |
| 85 | Dim source$, args$() |
| 86 | |
| 87 | Public Overrides Function ToString() As String |
| 88 | Return source |
| 89 | End Function |
| 90 | |
| 91 | Public Shared Operator <=(pattern As String, format As FormatHelper) As String |
| 92 | Return sprintf(pattern, format.args) |
| 93 | End Operator |
| 94 | |
| 95 | Public Shared Operator >=(pattern As String, format As FormatHelper) As String |
| 96 | Throw New NotSupportedException |
| 97 | End Operator |
| 98 | |
| 99 | Public Shared Operator <=(format As FormatHelper, args As String()) As String |
| 100 | Return sprintf(format.source, args) |
| 101 | End Operator |
| 102 | |
| 103 | Public Shared Operator >=(format As FormatHelper, args As String()) As String |
| 104 | Throw New NotSupportedException |
| 105 | End Operator |
| 106 | End Structure |
| 107 | End Namespace |