KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: A utility to convert straight double-quote pairs to curly quote pairs
PRODUCT: 4D | VERSION: 14.3 | PLATFORM: Mac & Win
Published On: January 30, 2015


This utility Replace straight double quote pairs, "as shown here," with HTML curly qupte entities (“, “ or “) and (”, ” or ”), “as shown here.”

The snippet below demonstrates how to used the utility.

$Text_T:=Document to text(Get 4D folder(HTML Root folder)+"Misc"+Folder separator+"SalesRep_Email.txt")
$HTML_TEXT_T:=UTIL_ConvertToCurlyQuotes ($Text_T;"“";"”")


Because replacements have to take place at specific locations with uneven string lengths, the commands Delete string and Insert string are used instead of Replace string.

UTIL_ConvertToCurlyQuotes


If (True)
   If (False)
      Begin SQL
     /*
      Name: UTIL_ConvertToCurlyQuotes
      Path: UTIL_ConvertToCurlyQuotes

      Purpose: Replace straight double quote pairs, "as shown here," with
      html curly qupte entities, “as shown here.”

      $0 - C_TEXT - Resulting text
      $1 - C_TEXT - Text containing double-quote pairs
      $2 - C_STRING - Left quote replacement HTML entity
      $3 - C_STRING - Right quote replacement HTML entity
     */
      End SQL
   End if
   C_TEXT($MethodName_T)
   $MethodName_T:=Current method name
      //===================== Declare Variables ==================================
      //method_parameters_declarations
   C_TEXT($0;$Txt_T;$1)
   C_STRING(20;$LEFT_DBL_QUOTE_ENTITY;$2)
   C_STRING(20;$RIGHT_DBL_QUOTE_ENTITY;$3)
      //--------------------------------------------------------------------------
      //method_wide_constants_declarations
      //--------------------------------------------------------------------------
      //local_variable_declarations
   C_LONGINT($Ndx;$SOA;$RIS;$Params_L)
   C_STRING(3;$LEFT_DBL_QUOTE_ENTITY;$RIGHT_DBL_QUOTE_ENTITY)
End if
  //====================== Initialize and Setup ================================

$Params_L:=Count parameters

$Txt_T:=$1
$LEFT_DBL_QUOTE_ENTITY:=$2
$RIGHT_DBL_QUOTE_ENTITY:=$3

ARRAY LONGINT($Pos_aL;0)
ARRAY LONGINT($Len_aL;0)

  //======================== Method Actions ==================================
  // Match string "(\")[^\"]*(\")" explained
  //
  // A leading, or left, quote (\") expressed as a regex group
  // followed by any number of characters not a quote [^\"]*
  // with an ending, or right, quote (\") expressed as a regex group
  //
  // Two "groups" create two elements in the provided arrays, see Tech Tip:
  // How to use array parameters with Match regex https://kb.4d.com/assetid=76087
  //
$Ndx:=1
While (Match regex("(\")[^\"]*(\")";$Txt_T;$Ndx;$Pos_aL;$Len_aL))
    // Replace the right group first because it's offset
    // would be invalid if left group is relaced first.
    //
   $Txt_T:=Delete string($Txt_T;$Pos_aL{2};$Len_aL{2})
   $Txt_T:=Insert string($Txt_T;$RIGHT_DBL_QUOTE_ENTITY;$Pos_aL{2})

   $Txt_T:=Delete string($Txt_T;$Pos_aL{1};$Len_aL{1})
   $Txt_T:=Insert string($Txt_T;$LEFT_DBL_QUOTE_ENTITY;$Pos_aL{1})
   $Ndx:=$Pos_aL{2}+Length($LEFT_DBL_QUOTE_ENTITY)+Length($RIGHT_DBL_QUOTE_ENTITY)
End while

  //======================== Clean up and Exit =================================

$0:=$Txt_T

See Also: