package org.jivesoftware.smack.util;

import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Random;

public class StringUtils {
   public static final String MD5 = "MD5";
   public static final String SHA1 = "SHA-1";
   public static final String UTF8 = "UTF-8";
   public static final String USASCII = "US-ASCII";
   public static final String QUOTE_ENCODE = "&quot;";
   public static final String APOS_ENCODE = "&apos;";
   public static final String AMP_ENCODE = "&amp;";
   public static final String LT_ENCODE = "&lt;";
   public static final String GT_ENCODE = "&gt;";
   public static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
   private static Random randGen = new Random();
   private static char[] numbersAndLetters = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

   public static CharSequence escapeForXML(String string) {
      if (string == null) {
         return null;
      } else {
         char[] input = string.toCharArray();
         int len = input.length;
         StringBuilder out = new StringBuilder((int)((double)len * 1.3D));
         int last = 0;
         int i = 0;

         while(i < len) {
            CharSequence toAppend = null;
            char ch = input[i];
            switch(ch) {
            case '"':
               toAppend = "&quot;";
               break;
            case '&':
               toAppend = "&amp;";
               break;
            case '\'':
               toAppend = "&apos;";
               break;
            case '<':
               toAppend = "&lt;";
               break;
            case '>':
               toAppend = "&gt;";
            }

            if (toAppend != null) {
               if (i > last) {
                  out.append(input, last, i - last);
               }

               out.append(toAppend);
               ++i;
               last = i;
            } else {
               ++i;
            }
         }

         if (last == 0) {
            return string;
         } else {
            if (i > last) {
               out.append(input, last, i - last);
            }

            return out;
         }
      }
   }

   /** @deprecated */
   @Deprecated
   public static synchronized String hash(String data) {
      return org.jivesoftware.smack.util.SHA1.hex(data);
   }

   public static String encodeHex(byte[] bytes) {
      char[] hexChars = new char[bytes.length * 2];

      for(int j = 0; j < bytes.length; ++j) {
         int v = bytes[j] & 255;
         hexChars[j * 2] = HEX_CHARS[v >>> 4];
         hexChars[j * 2 + 1] = HEX_CHARS[v & 15];
      }

      return new String(hexChars);
   }

   public static byte[] toBytes(String string) {
      try {
         return string.getBytes("UTF-8");
      } catch (UnsupportedEncodingException var2) {
         throw new IllegalStateException("UTF-8 encoding not supported by platform", var2);
      }
   }

   public static String randomString(int length) {
      if (length < 1) {
         return null;
      } else {
         char[] randBuffer = new char[length];

         for(int i = 0; i < randBuffer.length; ++i) {
            randBuffer[i] = numbersAndLetters[randGen.nextInt(numbersAndLetters.length)];
         }

         return new String(randBuffer);
      }
   }

   public static boolean isNotEmpty(CharSequence cs) {
      return !isNullOrEmpty(cs);
   }

   public static boolean isNullOrEmpty(CharSequence cs) {
      return cs == null || isEmpty(cs);
   }

   public static boolean isEmpty(CharSequence cs) {
      return cs.length() == 0;
   }

   public static String collectionToString(Collection<String> collection) {
      StringBuilder sb = new StringBuilder();
      Iterator i$ = collection.iterator();

      while(i$.hasNext()) {
         String s = (String)i$.next();
         sb.append(s);
         sb.append(" ");
      }

      String res = sb.toString();
      res = res.substring(0, res.length() - 1);
      return res;
   }

   public static String returnIfNotEmptyTrimmed(String string) {
      if (string == null) {
         return null;
      } else {
         String trimmedString = string.trim();
         return trimmedString.length() > 0 ? trimmedString : null;
      }
   }

   public static boolean nullSafeCharSequenceEquals(CharSequence csOne, CharSequence csTwo) {
      return nullSafeCharSequenceComperator(csOne, csTwo) == 0;
   }

   public static int nullSafeCharSequenceComperator(CharSequence csOne, CharSequence csTwo) {
      if (csOne == null ^ csTwo == null) {
         return csOne == null ? -1 : 1;
      } else {
         return csOne == null && csTwo == null ? 0 : csOne.toString().compareTo(csTwo.toString());
      }
   }

   public static <CS extends CharSequence> CS requireNotNullOrEmpty(CS cs, String message) {
      if (isNullOrEmpty(cs)) {
         throw new IllegalArgumentException(message);
      } else {
         return cs;
      }
   }

   public static String maybeToString(CharSequence cs) {
      return cs == null ? null : cs.toString();
   }
}
