|
| 1 | +/* -*-LCB-*- |
| 2 | +Copyright (C) 2016 LiveCode Ltd. |
| 3 | + |
| 4 | +This file is part of LiveCode. |
| 5 | + |
| 6 | +LiveCode is free software; you can redistribute it and/or modify it under |
| 7 | +the terms of the GNU General Public License v3 as published by the Free |
| 8 | +Software Foundation. |
| 9 | + |
| 10 | +LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | +WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | +for more details. |
| 14 | + |
| 15 | +You should have received a copy of the GNU General Public License |
| 16 | +along with LiveCode. If not see <http://www.gnu.org/licenses/>. |
| 17 | +*/ |
| 18 | + |
| 19 | +/** |
| 20 | +Utility functions for LiveCode Script-compatible item lists. |
| 21 | + |
| 22 | +Many LiveCode Builder widget and library extensions expose APIs to |
| 23 | +LiveCode Script that use item lists. This library provides a set of |
| 24 | +functions for converting `List` values to-and-from comma-delimited |
| 25 | +item strings. |
| 26 | +*/ |
| 27 | + |
| 28 | +module com.livecode.library.scriptitems |
| 29 | + |
| 30 | +metadata version is "1.0.0" |
| 31 | +metadata author is "LiveCode" |
| 32 | +metadata title is "LiveCode Script item list handling" |
| 33 | + |
| 34 | +/** |
| 35 | +Summary: Parse a comma-delimited "item" string as a list of strings |
| 36 | + |
| 37 | +Parameters: |
| 38 | +pStringValue: A string containing comma-delimited "items" |
| 39 | +pListLength: The number of elements the parsed list should contain |
| 40 | +pDefaultValue: Value to be used for extra elements |
| 41 | + |
| 42 | +Returns: The parsed items as a list of strings |
| 43 | + |
| 44 | +Description: |
| 45 | +Helper function for converting a LiveCode Script "item" list into list |
| 46 | +of strings, obeying the LiveCode Script rules for items. |
| 47 | + |
| 48 | +If <pListLength> is provided, then the returned list will always |
| 49 | +contain that number of elements: extra elements will be discarded, and |
| 50 | +missing elements will be set to <pDefaultValue>. |
| 51 | + |
| 52 | +Related: parseItemsAsNumberList (handler), formatStringListAsItems (handler) |
| 53 | +*/ |
| 54 | +public handler parseItemsAsStringList(in pStringValue as String, \ |
| 55 | + in pListLength as optional Number, \ |
| 56 | + in pDefaultValue as optional String) returns List /* of String */ |
| 57 | + |
| 58 | + variable tItems as List |
| 59 | + split pStringValue by "," into tItems |
| 60 | + |
| 61 | + if pListLength is not nothing then |
| 62 | + repeat while the number of elements in tItems < pListLength |
| 63 | + push pDefaultValue onto tItems |
| 64 | + end repeat |
| 65 | + if the number of elements in tItems > pListLength then |
| 66 | + delete element (pListLength + 1) to -1 of tItems |
| 67 | + end if |
| 68 | + end if |
| 69 | + |
| 70 | + return tItems |
| 71 | +end handler |
| 72 | + |
| 73 | +/** |
| 74 | +Summary: Parse a comma-delimited "item" string as a list of numbers |
| 75 | + |
| 76 | +Parameters: |
| 77 | +pStringValue: A string containing comma-delimited "items" |
| 78 | +pListLength: The number of elements the parsed list should contain |
| 79 | +pDefaultValue: Value to be used for extra elements |
| 80 | + |
| 81 | +Returns: The parsed items as a list of strings |
| 82 | + |
| 83 | +Description: |
| 84 | +Helper function for converting a LiveCode Script "item" list into a |
| 85 | +list of numbers, obeying the LiveCode Script rules for items. |
| 86 | + |
| 87 | +If <pListLength> is provided, then the returned list will always |
| 88 | +contain that number of elements: extra elements will be discarded, and |
| 89 | +missing elements will be set to <pDefaultValue>. |
| 90 | + |
| 91 | +Related: parseItemsAsStringList (handler), formatNumberListAsItems (handler) |
| 92 | +*/ |
| 93 | +public handler parseItemsAsNumberList(in pStringValue as String, \ |
| 94 | + in pListLength as optional Number, \ |
| 95 | + in pDefaultValue as optional Number) returns List /* of Number */ |
| 96 | + variable tItems as List |
| 97 | + put parseItemsAsStringList(pStringValue, pListLength, nothing) into tItems |
| 98 | + |
| 99 | + variable tNumbers as List |
| 100 | + variable tItem as optional String |
| 101 | + repeat for each element tItem in tItems |
| 102 | + variable tValue as Number |
| 103 | + if tItem is nothing then |
| 104 | + put pDefaultValue into tValue |
| 105 | + else |
| 106 | + put tItem parsed as number into tValue |
| 107 | + end if |
| 108 | + push tValue onto tNumbers |
| 109 | + end repeat |
| 110 | + |
| 111 | + return tNumbers |
| 112 | +end handler |
| 113 | + |
| 114 | +/** |
| 115 | +Summary: Format a list of strings as a comma-delimited "item" string |
| 116 | + |
| 117 | +Parameters: |
| 118 | +pList: A list of strings |
| 119 | + |
| 120 | +Returns: A comma-delimited LiveCode Script-compatible "item" string |
| 121 | + |
| 122 | +Description: |
| 123 | +Helper function for converting a list of strings into a LiveCode |
| 124 | +Script "item" list, obeying the LiveCode Script rules for items. |
| 125 | + |
| 126 | +Related: parseItemsAsStringList (handler), formatNumberListAsItems (handler) |
| 127 | +*/ |
| 128 | +public handler formatStringListAsItems(in pList as List) returns String |
| 129 | + if pList is empty then |
| 130 | + return "" |
| 131 | + end if |
| 132 | + |
| 133 | + variable tResult as String |
| 134 | + combine pList with "," into tResult |
| 135 | + |
| 136 | + -- If a LiveCode Script string ends with a delimiter, it does _not_ |
| 137 | + -- mean that the last item in the string is empty. To achieve |
| 138 | + -- that, it's necessary to add an extra delimiter. |
| 139 | + if tResult is empty or tResult ends with "," then |
| 140 | + put "," after tResult |
| 141 | + end if |
| 142 | + |
| 143 | + return tResult |
| 144 | +end handler |
| 145 | + |
| 146 | +/** |
| 147 | +Summary: Format a list of numbers as a comma-delimited "item" string |
| 148 | + |
| 149 | +Parameters: |
| 150 | +pList: A list of numbers |
| 151 | + |
| 152 | +Returns: A comma-delimited LiveCode Script-compatible "item" string |
| 153 | + |
| 154 | +Description: |
| 155 | +Helper function for converting a list of numbers into a LiveCode |
| 156 | +Script "item" list, obeying the LiveCode Script rules for items. |
| 157 | + |
| 158 | +Related: formatStringListAsItems (handler), parseItemsAsNumberList (handler) |
| 159 | +*/ |
| 160 | +public handler formatNumberListAsItems(in pList as List) returns String |
| 161 | + variable tItems as List |
| 162 | + variable tValue as Number |
| 163 | + repeat for each element tValue in pList |
| 164 | + push tValue formatted as string onto tItems |
| 165 | + end repeat |
| 166 | + return formatStringListAsItems(tItems) |
| 167 | +end handler |
| 168 | + |
| 169 | +end module |
0 commit comments