Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //signHelper.cfc
- component accessors="true" {
- property name="message";
- property name="width";
- function getSignInfo(required string message, required numeric width){
- var result = {};
- var signEntities = {};
- var currentEntity = "";
- var neededLines = "";
- var wordArray = "";
- var extraHyphenCount = 0;
- var tempArray = [""];
- var lines = [""];
- var i = "";
- this.setMessage(arguments.message);
- this.setWidth(arguments.width);
- wordArray = listToArray(this.getMessage()," ");
- //We need to handle words that might be longer than our sign can handle
- for(i = 1; i <= arrayLen(wordArray); i++){
- if(len(wordArray[i]) > this.getWidth()){
- //determine how many lines the word would span if we were hyphenating it
- neededLines = ceiling(len(wordArray[i])/(this.getWidth() - 1));
- //add each hypehnated segement to our array
- for(x = 1; x <= neededLines; x++){
- tempArray[arrayLen(tempArray)+1] = mid(wordArray[i],(x-1 != 0) ? (x-1)*this.getWidth() : 1, this.getWidth()) & ((x == neededLines) ? "" : "-");
- //we also need to increment our "-" count which will not be taken in account below
- if(!structKeyExists(signEntities,"-")){
- signEntities["-"] = 1;
- } else {
- signEntities["-"]++;
- }
- }
- } else {
- tempArray[arrayLen(tempArray)+1] = wordArray[i];
- }
- }
- wordArray = tempArray;
- for(i = 1; i <= len(this.getMessage()); i++){
- currentEntity = mid(this.getMessage(),i,1);
- //Not sure if we need a "space" character for the sign but assuming not i'm ignoring it
- if(currentEntity == " ")
- continue;
- //if we decide to count spaces we can comment out the continue and uncomment the next line,
- //guessing " " isnt a great struct key ;o so replace it with an _
- //currentEntity = "_";
- //If our key exists increment the count otherwise create it and set it to 1
- if(!structKeyExists(signEntities,"#currentEntity#")){
- signEntities["#currentEntity#"] = 1;
- } else {
- signEntities["#currentEntity#"]++;
- }
- }
- //loop over our word array
- for(i = 1; i <= arrayLen(wordArray); i++){
- //if the current data on the line + a space + the length of the next word is less than the message
- //width then we can add the word to the current line, otherwise we need to add another line
- if((len(lines[arrayLen(lines)]) + len(wordArray[i]) + 1) < this.getWidth()){
- lines[arrayLen(lines)] &= " " & wordArray[i];
- } else {
- lines[arrayLen(lines)+1] = wordArray[i];
- }
- }
- result.letterCounts = signEntities;
- result.displayLines = lines;
- return result;
- }
- }
- //index.cfm
- <cfscript>
- carwash = new signHelper();
- theSign = carwash.getSignInfo("COME ONE COME ALL, WE'RE PRACTICALLY GIVING CAR WASHES AWAY!",20);
- writeDump(theSign);
- </cfscript>
Advertisement
Add Comment
Please, Sign In to add comment