Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <cffunction name="getSignInfo" returntype="Struct">
- <cfargument name="message" type="string" required="true">
- <cfargument name="width" type="numeric" required="true">
- <cfargument name="includeSpaces" type="boolean" required="false" default="false"/>
- <cfargument name="fixWidthIssues" type="boolean" required="false" default="true">
- <cfset var signDetails = {}/>
- <cfset var adjustedMessage = trim(arguments.message)/>
- <cfset var charArr = listtoARray(adjustedMessage,'')/>
- <cfset var wordArr = listtoARray(adjustedMessage,' ')/>
- <cfset var i = ""/>
- <cfset var wordLength = ""/>
- <cfset var currentRow = 1/>
- <cfset var slotsFilled = 0/>
- <!---
- Examine each word in the message.
- If a word is too long for the width of the sign, we'll add spaces to adjust the message (Basically creating a "word wrap" feature)
- Optionally, we can throw an error when this happens.
- --->
- <cfloop from="1" to="#arraylen(wordArr)#" index="i">
- <cfset wordLength = len(wordarr[i])/>
- <cfif wordLength gt arguments.width>
- <cfif arguments.fixWidthIssues>
- <cfset wordArr[i] = left(wordArr[i],arguments.width) & " " & mid(wordArr[i],arguments.width,wordLength)/>
- <cfelse>
- <cfthrow message="Oh noes! One of your words is too long for the sign!"/>
- </cfif>
- </cfif>
- </cfloop>
- <cfset adjustedMessage = arrayToList(wordArr," ")/>
- <!--- Loop and count each letter. We can also return the number of spaces if desired --->
- <cfset signDetails.letterData = {}/>
- <cfloop array="#charArr#" index="i">
- <cfif i eq " " && !arguments.includeSpaces>
- <cfcontinue/>
- </cfif>
- <cfif !structkeyexists(signDetails.letterData,i)>
- <cfset signDetails.letterData[i] = 0/>
- </cfif>
- <cfset signDetails.letterData[i] += 1/>
- </cfloop>
- <cfset signDetails.rowData = {}/>
- <cfset signDetails.rowData["row1"] = ""/>
- <!--- Let's fill those rows --->
- <cfloop list="#adjustedMessage#" index="i" delimiters=" ">
- <cfif (wordlength + slotsFilled) gt arguments.width> <!--- No more room, next row please. --->
- <cfset currentRow++/>
- <cfset signDetails.rowData["row" & currentRow] = ""/>
- <cfset slotsFilled = 0/>
- </cfif>
- <cfset signDetails.rowData["row" & currentRow] &= i/>
- <cfset slotsFilled += wordLength/>
- <cfif slotsFilled lt arguments.width>
- <cfset signDetails.rowData["row" & currentRow] &= " "/> <!--- Add spaces between wods on the same row --->
- </cfif>
- </cfloop>
- <cfreturn signDetails/>
- </cffunction>
Advertisement
Add Comment
Please, Sign In to add comment