text:
// This code is based on ConvertUTF.c and ConvertUTF.h released by Unicode, Inc.

/*
 * Copyright 2001-2004 Unicode, Inc.
 *
 * Disclaimer
 *
 * This source code is provided as is by Unicode, Inc. No claims are
 * made as to fitness for any particular purpose. No warranties of any
 * kind are expressed or implied. The recipient agrees to determine
 * applicability of information provided. If this file has been
 * purchased on magnetic or optical media from Unicode, Inc., the
 * sole remedy for any claim will be exchange of defective media
 * within 90 days of receipt.
 *
 * Limitations on Rights to Redistribute This Code
 *
 * Unicode, Inc. hereby grants the right to freely use the information
 * supplied in this file in the creation of products supporting the
 * Unicode Standard, and to make copies of this file in any form
 * for internal or external distribution as long as this notice
 * remains attached.
 */

#include <cstring>
#include <cstdlib>
#include "unicode_utils.h"
namespace std {
template class std::basic_string<ggadget::UTF16Char>;
template class std::basic_string<ggadget::UTF32Char>;
}

namespace ggadget {
typedef unsigned char UTF8Char;
static const int kHalfShift = 10;
static const UTF32Char kHalfBase = 0x0010000UL;
static const UTF32Char kHalfMask = 0x3FFUL;

static const UTF32Char kSurrogateHighStart = 0xD800;
static const UTF32Char kSurrogateHighEnd = 0xDBFF;
static const UTF32Char kSurrogateLowStart = 0xDC00;
static const UTF32Char kSurrogateLowEnd = 0xDFFF;

static const UTF8Char kTrailingBytesForUTF8[256] = {
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
};

static const UTF32Char kOffsetsFromUTF8[6] = {
  0x00000000UL, 0x00003080UL, 0x000E2080UL,
  0x03C82080UL, 0xFA082080UL, 0x82082080UL
};

static const UTF8Char kFirstByteMark[7] = {
  0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
};

static inline bool IsLegalUTF8CharInternal(const char *src, size_t length) {
  const UTF8Char *srcptr = reinterpret_cast<const UTF8Char*>(src);
  UTF8Char a;
  UTF8Char ch = *srcptr;
  srcptr += length;
  switch (length) {
    default: return false;
    // Everything else falls through when "true"...
    case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
    case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
    case 2: if ((a = (*--srcptr)) > 0xBF) return false;
      switch (ch) {
        // No fall-through in this inner switch
        case 0xE0: if (a < 0xA0) return false; break;
        case 0xED: if (a > 0x9F) return false; break;
        case 0xF0: if (a < 0x90) return false; break;
        case 0xF4: if (a > 0x8F) return false; break;
        default:   if (a < 0x80) return false;
      }
    case 1: if (ch >= 0x80 && ch < 0xC2) return false;
  }
  if (ch > 0xF4) return false;
  return true;
}

static inline size_t ConvertCharUTF8ToUTF32Internal(const char *src,
                                                    size_t src_length,
                                                    UTF32Char *dest) {
  const UTF8Char *p = reinterpret_cast<const UTF8Char*>(src);

  size_t extra_bytes = kTrailingBytesForUTF8[*p];
  if (extra_bytes >= src_length ||
      !IsLegalUTF8CharInternal(src, extra_bytes + 1)) {
    *dest = 0;
    return 0;
  }

  UTF32Char result = 0;
  // The cases all fall through.
  switch (extra_bytes) {
    case 5: result += *p++; result <<= 6;
    case 4: result += *p++; result <<= 6;
    case 3: result += *p++; result <<= 6;
    case 2: result += *p++; result <<= 6;
    case 1: result += *p++; result <<= 6;
    case 0: result += *p++;
  }
  result -= kOffsetsFromUTF8[extra_bytes];
  if (result > kUnicodeMaxLegalChar ||
      (result >= kSurrogateHighStart && result <= kSurrogateLowEnd))
    result = kUnicodeReplacementChar;
  *dest = result;
  return extra_bytes + 1;
}

size_t ConvertCharUTF8ToUTF32(const char *src, size_t src_length,
                              UTF32Char *dest) {
  if (!src || !*src || !src_length || !dest) {
    return 0;
  }
  return ConvertCharUTF8ToUTF32Internal(src, src_length, dest);
}

static inline size_t ConvertCharUTF32ToUTF8Internal(UTF32Char src,
                                                    char *dest,
                                                    size_t dest_length) {
  const UTF32Char kByteMask = 0xBF;
  const UTF32Char kByteMark = 0x80;

  if (src > kUnicodeMaxLegalChar ||
      (src >= kSurrogateHighStart && src <= kSurrogateLowEnd) ||
      !dest || !dest_length)
    return 0;

  UTF8Char *p = reinterpret_cast<UTF8Char*>(dest);

  size_t bytes_to_write = 0;
  if (src < 0x80)
    bytes_to_write = 1;
  else if (src < 0x800)
    bytes_to_write = 2;
  else if (src < 0x10000)
    bytes_to_write = 3;
  else
    bytes_to_write = 4;

  if (bytes_to_write > dest_length)
    return 0;

  p += bytes_to_write;
  // Everything falls through.
  switch (bytes_to_write) {
    case 4:
      *--p = static_cast<UTF8Char>((src | kByteMark) & kByteMask); src >>=6;
    case 3:
      *--p = static_cast<UTF8Char>((src | kByteMark) & kByteMask); src >>=6;
    case 2:
      *--p = static_cast<UTF8Char>((src | kByteMark) & kByteMask); src >>=6;
    case 1:
      *--p = static_cast<UTF8Char>(src | kFirstByteMark[bytes_to_write]);
  }

  return bytes_to_write;
}

size_t ConvertCharUTF32ToUTF8(UTF32Char src, char *dest, size_t dest_length) {
  if (!dest || !dest_length) {
    return 0;
  }
  return ConvertCharUTF32ToUTF8Internal(src, dest, dest_length);
}

static inline size_t ConvertCharUTF16ToUTF32Internal(const UTF16Char *src,
                                                     size_t src_length,
                                                     UTF32Char *dest) {
  UTF32Char high;
  high = *src;
  if (high >= kSurrogateHighStart && high <= kSurrogateHighEnd) {
    if (src_length > 1) {
      UTF32Char low = *(src+1);
      if (low >= kSurrogateLowStart && low <= kSurrogateLowEnd) {
        *dest = ((high - kSurrogateHighStart) << kHalfShift) +
                (low - kSurrogateLowStart) + kHalfBase;
        return 2;
      }
    }
    return 0;
  } else if (high >= kSurrogateLowStart && high <= kSurrogateLowEnd) {
    return 0;
  }
  *dest = high;
  return 1;
}

size_t ConvertCharUTF16ToUTF32(const UTF16Char *src, size_t src_length,
                               UTF32Char *dest) {
  if (!src || !*src || !src_length || !dest) {
    return 0;
  }
  return ConvertCharUTF16ToUTF32Internal(src, src_length, dest);
}

static inline size_t ConvertCharUTF32ToUTF16Internal(UTF32Char src,
                                                     UTF16Char *dest,
                                                     size_t dest_length) {
  if (src <= kUnicodeMaxBMPChar) {
    if (src >= kSurrogateHighStart && src <= kSurrogateLowEnd)
      return 0;
    *dest = static_cast<UTF16Char>(src);
    return 1;
  } else if (src <= kUnicodeMaxLegalChar && dest_length > 1) {
    src -= kHalfBase;
    *dest++ = static_cast<UTF16Char>((src >> kHalfShift) + kSurrogateHighStart);
    *dest = static_cast<UTF16Char>((src & kHalfMask) + kSurrogateLowStart);
    return 2;
  }
  return 0;
}

size_t ConvertCharUTF32ToUTF16(UTF32Char src, UTF16Char *dest,
                               size_t dest_length) {
  if (!dest || !dest_length)
    return 0;
  return ConvertCharUTF32ToUTF16Internal(src, dest, dest_length);
}

size_t ConvertStringUTF8ToUTF32(const char *src, size_t src_length,
                                UTF32String *dest) {
  if (!dest)
    return 0;
  dest->clear();
  if (!src || !src_length)
    return 0;

  dest->reserve(src_length);
  size_t used_length = 0;
  size_t utf8_len;
  UTF32Char utf32;
  while (src_length && *src) {
    utf8_len = ConvertCharUTF8ToUTF32Internal(src, src_length, &utf32);
    if (!utf8_len) break;
    dest->push_back(utf32);
    used_length += utf8_len;
    src += utf8_len;
    src_length -= utf8_len;
  }
  return used_length;
}

size_t ConvertStringUTF8ToUTF32(const std::string &src, UTF32String *dest) {
  return ConvertStringUTF8ToUTF32(src.c_str(), src.length(), dest);
}

size_t ConvertStringUTF32ToUTF8(const UTF32Char *src, size_t src_length,
                                std::string *dest) {
  if (!dest)
    return 0;
  dest->clear();
  if (!src || !src_length)
    return 0;

  dest->reserve(src_length * 3);
  size_t used_length = 0;
  size_t utf8_len;
  char utf8[6];
  while (src_length && *src) {
    utf8_len = ConvertCharUTF32ToUTF8Internal(*src, utf8, 6);
    if (!utf8_len) break;
    dest->append(utf8, utf8_len);
    ++used_length;
    ++src;
    --src_length;
  }
  return used_length;
}

size_t ConvertStringUTF32ToUTF8(const UTF32String &src, std::string *dest) {
  return ConvertStringUTF32ToUTF8(src.c_str(), src.length(), dest);
}

size_t ConvertStringUTF8ToUTF16(const char *src, size_t src_length,
                                UTF16String *dest) {
  if (!dest)
    return 0;
  dest->clear();
  if (!src || !src_length)
    return 0;

  dest->reserve(src_length);
  size_t used_length = 0;
  size_t utf8_len;
  size_t utf16_len;
  UTF16Char utf16[2];
  UTF32Char utf32;
  while (src_length && *src) {
    utf8_len = ConvertCharUTF8ToUTF32Internal(src, src_length, &utf32);
    if (!utf8_len) break;
    utf16_len = ConvertCharUTF32ToUTF16Internal(utf32, utf16, 2);
    if (!utf16_len) break;
    dest->append(utf16, utf16_len);
    used_length += utf8_len;
    src += utf8_len;
    src_length -= utf8_len;
  }
  return used_length;
}

size_t ConvertStringUTF8ToUTF16(const std::string &src, UTF16String *dest) {
  return ConvertStringUTF8ToUTF16(src.c_str(), src.length(), dest);
}

size_t ConvertStringUTF8ToUTF16Buffer(const char *src, size_t src_length,
                                      UTF16Char *dest, size_t dest_length,
                                      size_t *used_dest_length) {
  if (!used_dest_length)
    return 0;
  *used_dest_length = 0;
  if (!dest || !dest_length || !src || !*src)
    return 0;

  size_t used_src_length = 0;
  size_t utf8_len;
  size_t utf16_len;
  UTF16Char utf16[2] = { 0, 0 };
  UTF32Char utf32;
  while (src_length && *src) {
    utf8_len = ConvertCharUTF8ToUTF32Internal(src, src_length, &utf32);
    if (!utf8_len) break;
    if (dest_length >= 2) {
      utf16_len = ConvertCharUTF32ToUTF16Internal(utf32, dest, 2);
      if (!utf16_len) break;
    } else {
      utf16_len = ConvertCharUTF32ToUTF16Internal(utf32, utf16, 2);
      if (!utf16_len || utf16_len > dest_length)
        break;
      dest[0] = utf16[0];
      if (utf16_len == 2)
        dest[1] = utf16[1];
    }
    dest += utf16_len;
    dest_length -= utf16_len;
    *used_dest_length += utf16_len;
    used_src_length += utf8_len;
    src += utf8_len;
    src_length -= utf8_len;
  }
  return used_src_length;
}

size_t ConvertStringUTF8ToUTF16Buffer(const std::string &src,
                                      UTF16Char *dest, size_t dest_length,
                                      size_t *used_dest_length) {
  return ConvertStringUTF8ToUTF16Buffer(src.c_str(), src.length(),
                                        dest, dest_length, used_dest_length);
}

size_t ConvertStringUTF16ToUTF8(const UTF16Char *src, size_t src_length,
                                std::string *dest) {
  if (!dest)
    return 0;
  dest->clear();
  if (!src || !src_length)
    return 0;

  dest->reserve(src_length * 3);
  size_t used_length = 0;
  size_t utf8_len;
  size_t utf16_len;
  char utf8[6];
  UTF32Char utf32;
  while (src_length && *src) {
    utf16_len = ConvertCharUTF16ToUTF32Internal(src, src_length, &utf32);
    if (!utf16_len) break;
    utf8_len = ConvertCharUTF32ToUTF8Internal(utf32, utf8, 6);
    if (!utf8_len) break;
    dest->append(utf8, utf8_len);
    used_length += utf16_len;
    src += utf16_len;
    src_length -= utf16_len;
  }
  return used_length;
}

size_t ConvertStringUTF16ToUTF8(const UTF16String &src, std::string *dest) {
  return ConvertStringUTF16ToUTF8(src.c_str(), src.length(), dest);
}

size_t ConvertStringUTF16ToUTF8Buffer(const UTF16Char *src, size_t src_length,
                                      char *dest, size_t dest_length,
                                      size_t *used_dest_length) {
  if (!used_dest_length)
    return 0;
  *used_dest_length = 0;
  if (!dest || !dest_length || !src || !*src)
    return 0;

  size_t used_src_length = 0;
  size_t utf8_len;
  size_t utf16_len;
  char utf8[4] = { 0, 0, 0, 0 };
  UTF32Char utf32;
  while (src_length && *src) {
    utf16_len = ConvertCharUTF16ToUTF32Internal(src, src_length, &utf32);
    if (!utf16_len) break;
    if (dest_length >= 4) {
      utf8_len = ConvertCharUTF32ToUTF8Internal(utf32, dest, 4);
      if (!utf8_len) break;
    } else {
      utf8_len = ConvertCharUTF32ToUTF8Internal(utf32, utf8, 4);
      if (!utf8_len || utf8_len > dest_length)
        break;
      memcpy(dest, utf8, utf8_len);
    }
    dest += utf8_len;
    dest_length -= utf8_len;
    *used_dest_length += utf8_len;
    used_src_length += utf16_len;
    src += utf16_len;
    src_length -= utf16_len;
  }
  return used_src_length;
}

size_t ConvertStringUTF16ToUTF8Buffer(const UTF16String &src,
                                      char *dest, size_t dest_length,
                                      size_t *used_dest_length) {
  return ConvertStringUTF16ToUTF8Buffer(src.c_str(), src.length(),
                                        dest, dest_length, used_dest_length);
}

void UTF16ToUTF8Converter::DoConvert(const UTF16Char *src, size_t src_length) {
  size_t used = 0;
  if (ConvertStringUTF16ToUTF8Buffer(src, src_length, buffer_,
                                     arraysize(buffer_) - 1, &used) ==
      src_length) {
    // The static buffer can contain the whole result, so use it.
    ASSERT(used < arraysize(buffer_));
    buffer_[used] = 0;
  } else {
    buffer_[0] = 0;
    ConvertStringUTF16ToUTF8(src, src_length, &dynamic_buffer_);
  }
}

size_t ConvertStringUTF16ToUTF32(const UTF16Char *src, size_t src_length,
                                 UTF32String *dest) {
  if (!dest)
    return 0;
  dest->clear();
  if (!src || !src_length)
    return 0;

  dest->reserve(src_length);
  size_t used_length = 0;
  size_t utf16_len;
  UTF32Char utf32;
  while (src_length && *src) {
    utf16_len = ConvertCharUTF16ToUTF32Internal(src, src_length, &utf32);
    if (!utf16_len) break;
    dest->push_back(utf32);
    used_length += utf16_len;
    src += utf16_len;
    src_length -= utf16_len;
  }
  return used_length;
}

size_t ConvertStringUTF16ToUTF32(const UTF16String &src, UTF32String *dest) {
  return ConvertStringUTF16ToUTF32(src.c_str(), src.length(), dest);
}

size_t ConvertStringUTF32ToUTF16(const UTF32Char *src, size_t src_length,
                                 UTF16String *dest) {
  if (!dest)
    return 0;
  dest->clear();
  if (!src || !src_length)
    return 0;

  dest->reserve(src_length);
  size_t used_length = 0;
  size_t utf16_len;
  UTF16Char utf16[2];
  while (src_length && *src) {
    utf16_len = ConvertCharUTF32ToUTF16Internal(*src, utf16, 2);
    if (!utf16_len) break;
    dest->append(utf16, utf16_len);
    ++used_length;
    ++src;
    --src_length;
  }
  return used_length;
}

size_t ConvertStringUTF32ToUTF16(const UTF32String &src, UTF16String *dest) {
  return ConvertStringUTF32ToUTF16(src.c_str(), src.length(), dest);
}

size_t GetUTF8CharLength(const char *src) {
  return src ?
         (kTrailingBytesForUTF8[static_cast<UTF8Char>(*src)] + 1) :
         0;
}

size_t GetUTF8CharsLength(const char *pos, size_t char_count, size_t limit) {
  size_t p = 0;
  for (size_t i = 0; i < char_count && p < limit; ++i) {
    p += GetUTF8CharLength(pos + p);
  }
  return p;
}

size_t GetUTF8StringCharCount(const char *src, size_t bytes) {
  size_t c = 0;
  while (c < bytes) {
    c += GetUTF8CharLength(src + c);
  }
  return c;
}

bool IsLegalUTF8Char(const char *src, size_t length) {
  if (!src || !length) return false;
  return IsLegalUTF8CharInternal(src, length);
}

size_t GetUTF16CharLength(const UTF16Char *src) {
  if (!src) return 0;
  if (*src < kSurrogateHighStart || *src > kSurrogateLowEnd)
    return 1;
  if (*src <= kSurrogateHighEnd &&
      *(src+1) >= kSurrogateLowStart && *(src+1) <= kSurrogateLowEnd)
    return 2;
  return 0;
}

bool IsLegalUTF16Char(const UTF16Char *src, size_t length) {
  return length && length == GetUTF16CharLength(src);
}

bool IsLegalUTF8String(const char *src, size_t length) {
  if (!src) return false;
  while (length > 0) {
    size_t char_length = GetUTF8CharLength(src);
    if (!char_length || char_length > length ||
        !IsLegalUTF8CharInternal(src, char_length))
      return false;
    length -= char_length;
    src += char_length;
  }
  return true;
}

bool IsLegalUTF8String(const std::string &src) {
  return IsLegalUTF8String(src.c_str(), src.length());
}

bool IsLegalUTF16String(const UTF16Char *src, size_t length) {
  if (!src) return false;
  while (length > 0) {
    size_t char_length = GetUTF16CharLength(src);
    if (!char_length || char_length > length ||
        !IsLegalUTF16Char(src, char_length))
      return false;
    length -= char_length;
    src += char_length;
  }
  return true;
}

bool IsLegalUTF16String(const UTF16String &src) {
  return IsLegalUTF16String(src.c_str(), src.length());
}

#define STARTS_WITH(content_ptr, content_size, pattern) \
    ((content_size) >= sizeof(pattern) && \
     memcmp((content_ptr), (pattern), sizeof(pattern)) == 0)

// A simple algorithm to detect BOM-less UTF-16 streams. It only applies to
// western languages that all UTF16 characters < 256.
// Returns 0 if stream is not UTF16, 1 if UTF16-LE, 2 if UTF16-BE.
static int DetectUTF16Encoding(const std::string &stream) {
  size_t content_size = stream.size();
  if (content_size == 0 || content_size % 2 != 0)
    return 0;

  int result = 0;
  for (size_t i = 0; i < content_size; i += 2) {
    char even = stream[i];
    char odd = stream[i + 1];
    if (!even) {
      if (!odd || result == 1)
        return 0;
      result = 2;
    } else if (!odd) {
      if (result == 2)
        return 0;
      result = 1;
    } else {
      return 0;
    }
  }
  return result;
}

bool DetectUTFEncoding(const std::string &stream, std::string *encoding) {
  ASSERT(encoding);
  const char *content_ptr = stream.c_str();
  size_t content_size = stream.size();
  if (STARTS_WITH(content_ptr, content_size, kUTF8BOM)) {
    if (encoding) *encoding = "UTF-8";
    return true;
  }
  if (STARTS_WITH(content_ptr, content_size, kUTF32LEBOM)) {
    if (encoding) *encoding = "UTF-32LE";
    return true;
  }
  if (STARTS_WITH(content_ptr, content_size, kUTF32BEBOM)) {
    if (encoding) *encoding = "UTF-32BE";
    return true;
  }
  if (STARTS_WITH(content_ptr, content_size, kUTF16LEBOM)) {
    if (encoding) *encoding = "UTF-16LE";
    return true;
  }
  if (STARTS_WITH(content_ptr, content_size, kUTF16BEBOM)) {
    if (encoding) *encoding = "UTF-16BE";
    return true;
  }

  // We don't check BOM-less UTF-8, because there is ambiguity among UTF-8 and
  // some CJK encodings. This function returns true only if the detection is
  // confidental.

  switch (DetectUTF16Encoding(stream)) {
    case 1:
      if (encoding) *encoding = "UTF-16LE";
      return true;
    case 2:
      if (encoding) *encoding = "UTF-16BE";
      return true;
    default:
      if (encoding) encoding->clear();
      return false;
  }
}

static void ConvertUTF16LEStreamToString(const char *input, size_t size,
                                         UTF16String *result) {
  ASSERT(result);
  result->clear();
  if (size < 2) return;
  result->reserve(size / 2);
  const unsigned char *stream = reinterpret_cast<const unsigned char *>(input);
  for (size_t i = 0; i < size - 1; i += 2)
    result->push_back(static_cast<UTF16Char>(stream[i] | (stream[i + 1] << 8)));
}

static void ConvertUTF16BEStreamToString(const char *input, size_t size,
                                         UTF16String *result) {
  ASSERT(result);
  result->clear();
  if (size < 2) return;
  result->reserve(size / 2);
  const unsigned char *stream = reinterpret_cast<const unsigned char *>(input);
  for (size_t i = 0; i < size - 1; i += 2)
    result->push_back(static_cast<UTF16Char>((stream[i] << 8) | stream[i + 1]));
}

static void ConvertUTF32LEStreamToString(const char *input, size_t size,
                                         UTF32String *result) {
  ASSERT(result);
  result->clear();
  if (size < 4) return;
  result->reserve(size / 4);
  const unsigned char *stream = reinterpret_cast<const unsigned char *>(input);
  for (size_t i = 0; i < size - 3; i += 4)
    result->push_back(static_cast<UTF32Char>(stream[i] | (stream[i + 1] << 8) |
                      (stream[i + 2] << 16) | (stream[i + 3] << 24)));
}

static void ConvertUTF32BEStreamToString(const char *input, size_t size,
                                         UTF32String *result) {
  ASSERT(result);
  result->clear();
  if (size < 4) return;
  result->reserve(size / 4);
  const unsigned char *stream = reinterpret_cast<const unsigned char *>(input);
  for (size_t i = 0; i < size - 3; i += 4)
    result->push_back(static_cast<UTF32Char>((stream[i] << 24) |
                      (stream[i + 1] << 16) | (stream[i + 2] << 8) |
                      stream[i + 3]));
}

bool DetectAndConvertStreamToUTF8(const std::string &stream,
                                  std::string *result, std::string *encoding) {
  ASSERT(result);
  const char *content_ptr = stream.c_str();
  size_t content_size = stream.size();
  bool valid = false;

  if (STARTS_WITH(content_ptr, content_size, kUTF8BOM)) {
    if (encoding) *encoding = "UTF-8";
    valid = IsLegalUTF8String(stream);
    if (valid) *result = stream.substr(sizeof(kUTF8BOM));
  } else if (STARTS_WITH(content_ptr, content_size, kUTF32LEBOM) &&
             content_size % 4 == 0) {
    if (encoding) *encoding = "UTF-32LE";
    UTF32String utf32;
    ConvertUTF32LEStreamToString(stream.c_str() + sizeof(kUTF32LEBOM),
                                 content_size - sizeof(kUTF32LEBOM), &utf32);
    valid = (ConvertStringUTF32ToUTF8(utf32, result) == utf32.size());
  } else if (STARTS_WITH(content_ptr, content_size, kUTF32BEBOM) &&
             content_size % 4 == 0) {
    if (encoding) *encoding = "UTF-32BE";
    UTF32String utf32;
    ConvertUTF32BEStreamToString(stream.c_str() + sizeof(kUTF32BEBOM),
                                 content_size - sizeof(kUTF32BEBOM), &utf32);
    valid = (ConvertStringUTF32ToUTF8(utf32, result) == utf32.size());
  } else if (STARTS_WITH(content_ptr, content_size, kUTF16LEBOM) &&
             content_size % 2 == 0) {
    if (encoding) *encoding = "UTF-16LE";
    UTF16String utf16;
    ConvertUTF16LEStreamToString(stream.c_str() + sizeof(kUTF16LEBOM),
                                 content_size - sizeof(kUTF16LEBOM), &utf16);
    valid = (ConvertStringUTF16ToUTF8(utf16, result) == utf16.size());
  } else if (STARTS_WITH(content_ptr, content_size, kUTF16BEBOM) &&
             content_size % 2 == 0) {
    if (encoding) *encoding = "UTF-16BE";
    UTF16String utf16;
    ConvertUTF16BEStreamToString(stream.c_str() + sizeof(kUTF16BEBOM),
                                 content_size - sizeof(kUTF16BEBOM), &utf16);
    valid = (ConvertStringUTF16ToUTF8(utf16, result) == utf16.size());
    // No BOM, try UTF-16 first
  } else {
    // The try BOM-less UTF-16.
    switch (DetectUTF16Encoding(stream)) {
      case 0:
        // Not UTF-16BE or UTF-16LE, try UTF-8.
        valid = IsLegalUTF8String(stream);
        if (valid) {
          if (encoding) *encoding = "UTF-8";
          *result = stream;
        }
        break;
      case 1: {
        UTF16String utf16;
        ConvertUTF16LEStreamToString(stream.c_str(), content_size, &utf16);
        if (encoding) *encoding = "UTF-16LE";
        valid = (ConvertStringUTF16ToUTF8(utf16, result) == utf16.size());
        break;
      }
      case 2: {
        UTF16String utf16;
        ConvertUTF16BEStreamToString(stream.c_str(), content_size, &utf16);
        if (encoding) *encoding = "UTF-16BE";
        valid = (ConvertStringUTF16ToUTF8(utf16, result) == utf16.size());
        break;
      }
      default:
        break;
    }
  }

  if (!valid) {
    if (encoding) *encoding = "ISO8859-1";
    // Not valid UTF-8, treat it as ISO8859-1.
    UTF16String utf16;
    utf16.reserve(content_size);
    for (size_t i = 0; i < content_size; i++)
      utf16 += static_cast<unsigned char>(content_ptr[i]);
    valid = (ConvertStringUTF16ToUTF8(utf16, result) == utf16.size());
  }

  if (!valid) {
    result->clear();
    if (encoding) encoding->clear();
  }
  return valid;
}

// Here we assume that if wchar_t is 2 bytes, the encoding is UTF16, and
// if wchar_t is 4 bytes, the encoding is UTF32.
#if GGL_SIZEOF_WCHAR_T != 2 && GGL_SIZEOF_WCHAR_T != 4
#error wchar_t must be 2 bytes or 4 bytes
#endif

bool ConvertLocaleStringToUTF16(const char *input, UTF16String *result) {
  ASSERT(input && result);
  if (!input || !result)
    return false;

  result->clear();
  size_t buffer_size = mbstowcs(NULL, input, 0) + 1;
  if (buffer_size == 0) // mbstowcs returns (size_t)-1 on error.
    return false;
  wchar_t *buffer(new wchar_t[buffer_size]);
  if (!buffer)
    return false;

  bool success = true;
  mbstowcs(buffer, input, buffer_size);
#if GGL_SIZEOF_WCHAR_T == 2
  result->assign(reinterpret_cast<UTF16Char *>(buffer));
#else
  success = ConvertStringUTF32ToUTF16(reinterpret_cast<UTF32Char *>(buffer),
                                      buffer_size - 1, result) ==
            buffer_size - 1;
#endif

  delete [] buffer;
  return success;
}

bool ConvertUTF16ToLocaleString(const UTF16Char *input, size_t input_size,
                                std::string *result) {
  ASSERT(input && result);
  if (!input || !result)
    return false;

  result->clear();
  if (!input_size)
    return true;

  const wchar_t *wchar_input = NULL;
#if GGL_SIZEOF_WCHAR_T == 2
  UTF16String utf16(input, input_size);
  wchar_input = utf16.c_str();
#else
  UTF32String utf32;
  if (ConvertStringUTF16ToUTF32(input, input_size, &utf32) != input_size)
    return false;
  wchar_input = reinterpret_cast<const wchar_t *>(utf32.c_str());
#endif

  size_t buffer_size = wcstombs(NULL, wchar_input, 0) + 1;
  if (buffer_size == 0) // wcstombs returns (size_t)-1 on error.
    return false;
  char *buffer(new char[buffer_size]);
  if (!buffer)
    return false;

  wcstombs(buffer, wchar_input, buffer_size);
  result->assign(buffer);
  delete [] buffer;
  return true;
}

bool ConvertUTF16ToLocaleString(const UTF16String &input, std::string *result) {
  return ConvertUTF16ToLocaleString(input.c_str(), input.size(), result);
}

bool ConvertLocaleStringToUTF8(const char *input, std::string *result) {
  ASSERT(input && result);
  if (!input || !result)
    return false;

  result->clear();
  size_t buffer_size = mbstowcs(NULL, input, 0) + 1;
  if (buffer_size == 0) // mbstowcs returns (size_t)-1 on error.
    return false;
  wchar_t *buffer(new wchar_t[buffer_size]);
  if (!buffer)
    return false;

  bool success = true;
  mbstowcs(buffer, input, buffer_size);
#if GGL_SIZEOF_WCHAR_T == 2
  success = ConvertStringUTF16ToUTF8(reinterpret_cast<UTF16Char *>(buffer),
                                     buffer_size - 1, result) ==
            buffer_size - 1;
#else
  success = ConvertStringUTF32ToUTF8(reinterpret_cast<UTF32Char *>(buffer),
                                     buffer_size - 1, result) ==
            buffer_size - 1;
#endif

  delete [] buffer;
  return success;
}

bool ConvertUTF8ToLocaleString(const char *input, size_t input_size,
                               std::string *result) {
  ASSERT(input && result);
  if (!input || !result)
    return false;

  result->clear();
  if (!input_size)
    return true;

  const wchar_t *wchar_input = NULL;
#if GGL_SIZEOF_WCHAR_T == 2
  UTF16String utf16;
  if (ConvertStringUTF8ToUTF16(input, input_size, &utf16) != input_size)
    return false;
  wchar_input = reinterpret_cast<const wchar_t *>(utf16.c_str());
#else
  UTF32String utf32;
  if (ConvertStringUTF8ToUTF32(input, input_size, &utf32) != input_size)
    return false;
  wchar_input = reinterpret_cast<const wchar_t *>(utf32.c_str());
#endif

  size_t buffer_size = wcstombs(NULL, wchar_input, 0) + 1;
  if (buffer_size == 0) // wcstombs returns (size_t)-1 on error.
    return false;
  char *buffer(new char[buffer_size]);
  if (!buffer)
    return false;

  wcstombs(buffer, wchar_input, buffer_size);
  result->assign(buffer);
  delete [] buffer;
  return true;
}

bool ConvertUTF8ToLocaleString(const std::string &input, std::string *result) {
  return ConvertUTF8ToLocaleString(input.c_str(), input.size(), result);
}

int CompareLocaleStrings(const char *s1, const char *s2) {
  return strcoll(s1, s2);
}

} // namespace ggadget


text:
if [ -f MQ2008.rar ]
then
    echo "Use downloaded data to run experiment."
else
    echo "Downloading data."
    wget https://s3-us-west-2.amazonaws.com/xgboost-examples/MQ2008.rar
    unrar x MQ2008.rar
    mv -f MQ2008/Fold1/*.txt .
fi

python trans_data.py train.txt mq2008.train mq2008.train.group

python trans_data.py test.txt mq2008.test mq2008.test.group

python trans_data.py vali.txt mq2008.vali mq2008.vali.group

redpajama_set_name: RedPajamaGithub


text:
<?php

namespace ProcessWire\GraphQL\Test\FieldtypePage;

/**
 * Test the case when FieldtypePage:derefAsPageOrFalse is set
 * and the value is empty.
 */

use ProcessWire\FieldtypePage;
use ProcessWire\GraphQL\Test\GraphQLTestCase;
use ProcessWire\GraphQL\Utils;

class CaseThreeTest extends GraphQLTestCase
{
  const settings = [
    "login" => "admin",
    "legalTemplates" => ["skyscraper", "architect"],
    "legalFields" => ["architects"],
    "access" => [
      "fields" => [
        [
          "name" => "architects",
          "derefAsPage" => \ProcessWire\FieldtypePage::derefAsPageOrFalse,
        ],
      ],
    ],
  ];

  public function testValue()
  {
    $skyscraper = Utils::pages()->get(
      "template=skyscraper, architects.count=0"
    );

    // wake up the architects value
    // which will set it to false
    $skyscraper->architects;

    $query = "{
  		skyscraper (s: \"id=$skyscraper->id\") {
  			list {
  				architects {
  					list {
  						id
  						name
  					}
  				}
  			}
  		}
  	}";
    $res = self::execute($query);
    self::assertEquals(
      0,
      count($res->data->skyscraper->list[0]->architects->list),
      "Returns empty architect page array."
    );

    self::assertObjectNotHasAttribute("errors", $res, "There are errors.");
  }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="is.xyz.mpv">

    <uses-feature
        android:name="android.software.leanback"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.touchscreen"
        android:required="false" />
    <uses-feature android:glEsVersion="0x00020000" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:resizeableActivity="true"
        android:icon="@mipmap/mpv_launcher_icon"
        android:roundIcon="@mipmap/mpv_launcher_icon"
        android:label="@string/mpv_activity">
        <activity
            android:name=".MPVActivity"
            android:configChanges="orientation|screenSize|keyboardHidden|keyboard"
            android:launchMode="singleTask"
            android:theme="@style/AppTheme">
            <intent-filter> <!-- Media protocols -->
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="rtmp" />
                <data android:scheme="rtmps" />
                <data android:scheme="rtp" />
                <data android:scheme="rtsp" />
                <data android:scheme="mms" />
                <data android:scheme="mmst" />
                <data android:scheme="mmsh" />
                <data android:scheme="tcp" />
                <data android:scheme="udp" />
            </intent-filter>
            <intent-filter> <!-- MIME type only (e.g. Google Drive) -->
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:mimeType="video/*" />
                <data android:mimeType="audio/*" />
            </intent-filter>
            <intent-filter> <!-- HTTP(S) with MIME type (#43) -->
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="http" />
                <data android:scheme="https" />
                <data android:host="*" />
                <data android:mimeType="video/*" />
                <data android:mimeType="audio/*" />
                <data android:mimeType="application/vnd.apple.mpegurl" />
            </intent-filter>
            <intent-filter> <!-- HTTP(S) with extension (e.g. Chrome) -->
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="http" />
                <data android:scheme="https" />
                <data android:host="*" />
                <!-- the duplicate patterns below work around an Android bug: -->
                <!-- http://stackoverflow.com/questions/3400072/#answer-8599921 -->
                <data android:pathPattern=".*\\.mkv" />
                <data android:pathPattern=".*\\..*\\.mkv" />
                <data android:pathPattern=".*\\..*\\..*\\.mkv" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\.mkv" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.mkv" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.mkv" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.mkv" />

                <data android:pathPattern=".*\\.mp4" />
                <data android:pathPattern=".*\\..*\\.mp4" />
                <data android:pathPattern=".*\\..*\\..*\\.mp4" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\.mp4" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.mp4" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.mp4" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.mp4" />

                <data android:pathPattern=".*\\.webm" />
                <data android:pathPattern=".*\\..*\\.webm" />
                <data android:pathPattern=".*\\..*\\..*\\.webm" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\.webm" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.webm" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.webm" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.webm" />

                <data android:pathPattern=".*\\.avi" />
                <data android:pathPattern=".*\\..*\\.avi" />
                <data android:pathPattern=".*\\..*\\..*\\.avi" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\.avi" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.avi" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.avi" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.avi" />

                <data android:pathPattern=".*\\.flac" />
                <data android:pathPattern=".*\\..*\\.flac" />
                <data android:pathPattern=".*\\..*\\..*\\.flac" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\.flac" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.flac" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.flac" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.flac" />

                <data android:pathPattern=".*\\.mp3" />
                <data android:pathPattern=".*\\..*\\.mp3" />
                <data android:pathPattern=".*\\..*\\..*\\.mp3" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\.mp3" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.mp3" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.mp3" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.mp3" />

                <data android:pathPattern=".*\\.ogg" />
                <data android:pathPattern=".*\\..*\\.ogg" />
                <data android:pathPattern=".*\\..*\\..*\\.ogg" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\.ogg" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.ogg" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.ogg" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.ogg" />

                <data android:pathPattern=".*\\.m3u8*" />
                <data android:pathPattern=".*\\..*\\.m3u8*" />
                <data android:pathPattern=".*\\..*\\..*\\.m3u8*" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\.m3u8*" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.m3u8*" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.m3u8*" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.m3u8*" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|screenSize"
            android:theme="@style/FilePickerTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SettingsActivity"
            android:label="@string/title_activity_settings" />
        <service android:name=".BackgroundPlaybackService" android:exported="false" />
        <receiver android:name=".NotificationButtonReceiver" android:exported="false">
            <intent-filter>
                <action android:name="is.xyz.mpv.ACTION_PREV" />
                <action android:name="is.xyz.mpv.ACTION_NEXT" />
            </intent-filter>
        </receiver>
    </application>

</manifest>


text:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.matshias.yoga2sharpnessfix" >

    <uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".SharpnessFixConfig"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".BroadcastIntentService" >
        </service>

        <receiver
            android:name=".ScreenOnReceiver"
            android:enabled="true"
            android:exported="true" >
        </receiver>
        <receiver
            android:name=".BootCompleteReceiver"
            android:enabled="false"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service
            android:name=".BackgroundService"
            android:enabled="true"
            android:exported="true" >
        </service>
    </application>

</manifest>


text:
package com.twu.biblioteca.menu_items;

public abstract class MenuItem {

    protected String name;
    public abstract void execute();

    public String getName() {
        return this.name;
    }
}


text:
var fs = require('fs');
var istanbul = require('browserify-istanbul');
var isparta = require('isparta');

// Use ENV vars on Travis and sauce.json locally to get credentials
if (!process.env.SAUCE_USERNAME) {
    if (!fs.existsSync('sauce.json')) {
        console.log('Create a sauce.json with your credentials based on the sauce-sample.json file.');
        process.exit(1);
    } else {
        process.env.SAUCE_USERNAME = require('./sauce').username;
        process.env.SAUCE_ACCESS_KEY = require('./sauce').accessKey;
    }
}

// Browsers to run on Sauce Labs
var customLaunchers = {
    'SL_Chrome': {
        base: 'SauceLabs',
        browserName: 'chrome',
        platform: 'Mac OS X 10.9'
    },

    'SL_IE': {
        base: 'SauceLabs',
        browserName: 'internet explorer',
        version: '9'
    },
    //
    //'SL_Firefox': {
    //    base: 'SauceLabs',
    //    browserName: 'firefox'
    //},

    //'SL_Safari': {
    //    base: 'SauceLabs',
    //    browserName: 'safari',
    //    varsion: '6',
    //    platform: 'Mac OS X 10.8'
    //},

    //'SL_iOS': {
    //    base: 'SauceLabs',
    //    browserName: 'iphone',
    //    version: '6.1'
    //},

    //'SL_Android': {
    //    base: 'SauceLabs',
    //    browserName: 'android'
    //}
};

module.exports = function (config) {
    'use strict';

    config.set({
        reporters: ['dots', 'saucelabs', 'coverage'],
        frameworks: ['browserify', 'mocha', 'chai'],

        // list of files / patterns to load in the browser
        files: ['./tests/spec/**/*.js'],

        preprocessors: {
            './tests/spec/**/*.js': ['browserify']
        },

        browserify: {
            debug: true,
            transform: [
                //'babelify',
                istanbul({
                    instrumenter: isparta,
                    ignore: ['**/node_modules/**', '**/test/**'],
                })
            ],
            configure: function (bundle) {
                bundle.on('bundled', function (error) {
                    if (error != null)
                        console.error(error.message);
                });
            }
        },

        coverageReporter: {
            reporters: [
                { type: 'lcovonly', subdir: '.', file: 'report-lcovonly.txt' },
                { type: 'text', subdir: '.', file: 'text.txt' },
                { type: 'text-summary', subdir: '.', file: 'text-summary.txt' },
            ]
        },

        //notifyReporter: {
        //    reportEachFailure: true,
        //    reportSuccess: true
        //},

        logLevel: config.LOG_INFO,
        color: true,

        sauceLabs: {
            testName: 'Dumpjs tests',
            build: 'TRAVIS #' + process.env.TRAVIS_BUILD_NUMBER + ' (' + process.env.TRAVIS_BUILD_ID + ')'
        },

        captureTimeout: 200000,
        customLaunchers: customLaunchers,

        browsers: Object.keys(customLaunchers),
        singleRun: true
    });
};


text:
  #define GL_SERVICE_LOG_CODE_BLOCK(code)
#else
  #define GL_SERVICE_LOG_CODE_BLOCK(code) code
#endif

// Forward declare OSMesa types.
typedef struct osmesa_context *OSMesaContext;
typedef void (*OSMESAproc)();

#if !defined(OS_MACOSX)

// Forward declare EGL types.
typedef unsigned int EGLBoolean;
typedef unsigned int EGLenum;
typedef int EGLint;
typedef void *EGLConfig;
typedef void *EGLContext;
typedef void *EGLDisplay;
typedef void *EGLImageKHR;
typedef void *EGLSurface;
typedef void *EGLClientBuffer;
typedef void (*__eglMustCastToProperFunctionPointerType)(void);
typedef void* GLeglImageOES;

#if defined(OS_WIN)
typedef HDC     EGLNativeDisplayType;
typedef HBITMAP EGLNativePixmapType;
typedef HWND    EGLNativeWindowType;
#elif defined(OS_ANDROID)
typedef void                       *EGLNativeDisplayType;
typedef struct egl_native_pixmap_t *EGLNativePixmapType;
typedef struct ANativeWindow       *EGLNativeWindowType;
#else
typedef Display *EGLNativeDisplayType;
typedef Pixmap   EGLNativePixmapType;
typedef Window   EGLNativeWindowType;
#endif

#endif  // !OS_MACOSX

#include "gl_bindings_autogen_gl.h"
#include "gl_bindings_autogen_osmesa.h"

#if defined(OS_WIN)
#include "gl_bindings_autogen_egl.h"
#include "gl_bindings_autogen_wgl.h"
#elif defined(USE_X11)
#include "gl_bindings_autogen_egl.h"
#include "gl_bindings_autogen_glx.h"
#elif defined(OS_ANDROID)
#include "gl_bindings_autogen_egl.h"
#endif

namespace gfx {

GL_EXPORT extern GLApi* g_current_gl_context;
GL_EXPORT extern OSMESAApi* g_current_osmesa_context;
GL_EXPORT extern DriverGL g_driver_gl;
GL_EXPORT extern DriverOSMESA g_driver_osmesa;

#if defined(OS_WIN)

GL_EXPORT extern EGLApi* g_current_egl_context;
GL_EXPORT extern WGLApi* g_current_wgl_context;
GL_EXPORT extern DriverEGL g_driver_egl;
GL_EXPORT extern DriverWGL g_driver_wgl;

#elif defined(USE_X11)

GL_EXPORT extern EGLApi* g_current_egl_context;
GL_EXPORT extern GLXApi* g_current_glx_context;
GL_EXPORT extern DriverEGL g_driver_egl;
GL_EXPORT extern DriverGLX g_driver_glx;

#elif defined(OS_ANDROID)

GL_EXPORT extern EGLApi* g_current_egl_context;
GL_EXPORT extern DriverEGL g_driver_egl;

#endif

// Find an entry point to the mock GL implementation.
void* GL_BINDING_CALL GetMockGLProcAddress(const char* name);

}  // namespace gfx

#endif  // UI_GL_GL_BINDINGS_H_


text:
package javax.swing.text.html;

import java.awt.*;
import java.text.BreakIterator;
import javax.swing.event.DocumentEvent;
import javax.swing.text.*;

/**
 * Displays the <dfn>inline element</dfn> styles
 * based upon css attributes.
 *
 * @author  Timothy Prinzing
 */
public class InlineView extends LabelView {

    /**
     * Constructs a new view wrapped on an element.
     *
     * @param elem the element
     */
    public InlineView(Element elem) {
        super(elem);
        StyleSheet sheet = getStyleSheet();
        attr = sheet.getViewAttributes(this);
    }

    /**
     * Gives notification that something was inserted into
     * the document in a location that this view is responsible for.
     * If either parameter is null, behavior of this method is
     * implementation dependent.
     *
     * @param e the change information from the associated document
     * @param a the current allocation of the view
     * @param f the factory to use to rebuild if the view has children
     * @since 1.5
     * @see View#insertUpdate
     */
    public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
        super.insertUpdate(e, a, f);
    }

    /**
     * Gives notification that something was removed from the document
     * in a location that this view is responsible for.
     * If either parameter is null, behavior of this method is
     * implementation dependent.
     *
     * @param e the change information from the associated document
     * @param a the current allocation of the view
     * @param f the factory to use to rebuild if the view has children
     * @since 1.5
     * @see View#removeUpdate
     */
    public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
        super.removeUpdate(e, a, f);
    }

    /**
     * Gives notification from the document that attributes were changed
     * in a location that this view is responsible for.
     *
     * @param e the change information from the associated document
     * @param a the current allocation of the view
     * @param f the factory to use to rebuild if the view has children
     * @see View#changedUpdate
     */
    public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
        super.changedUpdate(e, a, f);
        StyleSheet sheet = getStyleSheet();
        attr = sheet.getViewAttributes(this);
        preferenceChanged(null, true, true);
    }

    /**
     * Fetches the attributes to use when rendering.  This is
     * implemented to multiplex the attributes specified in the
     * model with a StyleSheet.
     */
    public AttributeSet getAttributes() {
        return attr;
    }

    /**
     * Determines how attractive a break opportunity in
     * this view is.  This can be used for determining which
     * view is the most attractive to call breakView
     * on in the process of formatting.  A view that represents
     * text that has whitespace in it might be more attractive
     * than a view that has no whitespace, for example.  The
     * higher the weight, the more attractive the break.  A
     * value equal to or lower than BadBreakWeight
     * should not be considered for a break.  A value greater
     * than or equal to ForcedBreakWeight should
     * be broken.
     *

     * This is implemented to provide the default behavior
     * of returning BadBreakWeight unless the length
     * is greater than the length of the view in which case the
     * entire view represents the fragment.  Unless a view has
     * been written to support breaking behavior, it is not
     * attractive to try and break the view.  An example of
     * a view that does support breaking is LabelView.
     * An example of a view that uses break weight is
     * ParagraphView.
     *
     * @param axis may be either View.X_AXIS or View.Y_AXIS
     * @param pos the potential location of the start of the
     *   broken view >= 0.  This may be useful for calculating tab
     *   positions.
     * @param len specifies the relative length from pos
     *   where a potential break is desired >= 0.
     * @return the weight, which should be a value between
     *   ForcedBreakWeight and BadBreakWeight.
     * @see LabelView
     * @see ParagraphView
     * @see View#BadBreakWeight
     * @see View#GoodBreakWeight
     * @see View#ExcellentBreakWeight
     * @see View#ForcedBreakWeight
     */
    public int getBreakWeight(int axis, float pos, float len) {
        if (nowrap) {
            return BadBreakWeight;
        }
        return super.getBreakWeight(axis, pos, len);
    }

    /**
     * Tries to break this view on the given axis. Refer to
     * {@link View#breakView} for a complete
     * description of this method.
     *
Behavior of this method is unspecified in case axis
     * is neither View.X_AXIS nor View.Y_AXIS, and
     * in case offset, pos, or len
     * is null.
     *
     * @param axis may be either View.X_AXIS or
     *          View.Y_AXIS
     * @param offset the location in the document model
     *   that a broken fragment would occupy >= 0.  This
     *   would be the starting offset of the fragment
     *   returned
     * @param pos the position along the axis that the
     *  broken view would occupy >= 0.  This may be useful for
     *  things like tab calculations
     * @param len specifies the distance along the axis
     *  where a potential break is desired >= 0
     * @return the fragment of the view that represents the
     *  given span.
     * @since 1.5
     * @see View#breakView
     */
    public View breakView(int axis, int offset, float pos, float len) {
        return super.breakView(axis, offset, pos, len);
    }

    /**
     * Set the cached properties from the attributes.
     */
    protected void setPropertiesFromAttributes() {
        super.setPropertiesFromAttributes();
        AttributeSet a = getAttributes();
        Object decor = a.getAttribute(CSS.Attribute.TEXT_DECORATION);
        boolean u = (decor != null) ?
          (decor.toString().indexOf("underline") >= 0) : false;
        setUnderline(u);
        boolean s = (decor != null) ?
          (decor.toString().indexOf("line-through") >= 0) : false;
        setStrikeThrough(s);
        Object vAlign = a.getAttribute(CSS.Attribute.VERTICAL_ALIGN);
        s = (vAlign != null) ? (vAlign.toString().indexOf("sup") >= 0) : false;
        setSuperscript(s);
        s = (vAlign != null) ? (vAlign.toString().indexOf("sub") >= 0) : false;
        setSubscript(s);

        Object whitespace = a.getAttribute(CSS.Attribute.WHITE_SPACE);
        if ((whitespace != null) && whitespace.equals("nowrap")) {
            nowrap = true;
        } else {
            nowrap = false;
        }

        HTMLDocument doc = (HTMLDocument)getDocument();
        // fetches background color from stylesheet if specified
        Color bg = doc.getBackground(a);
        if (bg != null) {
            setBackground(bg);
        }
    }

    protected StyleSheet getStyleSheet() {
        HTMLDocument doc = (HTMLDocument) getDocument();
        return doc.getStyleSheet();
    }

    private boolean nowrap;
    private AttributeSet attr;
}


text:
<!doctype html>

    <meta charset="utf-8">

    <title>Akka Talk</title>

    <meta name="description" content="Akka.io Overview">
    <meta name="author" content="Fede Silva">

    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"/>

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

    <link rel="stylesheet" href="css/reveal.min.css">
    <link rel="stylesheet" href="css/theme/moon.css" id="theme">

    <link rel="stylesheet" href="lib/css/zenburn.css">

    <link rel="stylesheet" href="css/custom.css">

Akka

a toolkit for concurrent systems

Federico Silva

                    @fedesilva /
                    slides at github

Akka

Federico Silva

One significant Akka App

Several smaller things in the middle

Now starting to build on Akka again

What is Akka?

            Akka.io

                    Akka is a toolkit and runtime
                    for building highly concurrent,
                    distributed, and resilient
                    message-driven applications
                    on the JVM.

What is Akka?

A toolkit

A runtime

JVM based

Written in Scala

What is Akka?

Concurrent

Parallel

What is Akka?

Concurrent

What is the innovation?

What makes it different and why is it important?

Why is concurrency important?

Modern CPUs

Many CPUs

Many Computers

Why do we need something new?

Need more control

Need clear semantics

Less coupling

Fault tolerance

Distribution?

Threads are not enough

Neither are thread pools

or Fork/Join

What is Akka?

What does it bring to the table?

A unified model for concurrency and distribution via the Actor Model

Actor Model

Old concept

"A Universal Modular Actor Formalism for Artificial Intelligence".

Carl Hewitt; Peter Bishop; Richard Steiger (1973).

Actor Model

                The actor model in computer science is a mathematical model of concurrent computation that treats
                "actors" as the universal primitives of concurrent computation: in response to a message that it
                receives, an actor can make local decisions, create more actors, send more messages, and determine how
                to respond to the next message received.

Actor Model

                Other implementations

Ruby, Celluloid

Erlang, language construct, OTP adds features

.NET, dotsero, Akka.net, MS Orleans

Actor Model

Message Passing

Location transparency

Isolation

Supervision

Monitoring

Dynamic behaviour

Message Passing

Is the basic building block, other features build upon it

Asynchronous delivery

Isolation

Location transparency

Isolation

Ability to control internal state, run without interference

Location Transparency

Ability to send a message without considering where the recipient is located

Supervision and Monitoring

Actor systems form hierarchies where each node is the supervisor of its children.

Supervision and Monitoring

Supervisors can delegate long running tasks

Worker nodes need not consider fault management

Cooperatively solve problems, under watch from supervisor

Supervisor gets messages when children die, can restart, kill others

Dynamic Behaviour

An actor can change its own behaviour in response to messages.

Can be though of as a state machine.

Actors in Akka

Actor Reference

State

Behavior

Mailbox

Children

Supervisor Strategy

When an Actor Terminates

Look at some code

Simple Akka Seed from Activator ©

App from template

Implement Switch Actor

Look at CounterActor

Other Akka modules

Cluster

Persistence

FSM

Camel

HTTP

Reactive Streams

Akka Cluster

Remoting

Routing

Load Balancing

Akka Persistence

Evented architectures, CQRS

Commands, backed by WAL

Persistence of state, with recovery

Reactive Streams

Better alternative, safer too

What is not SO good?

What is not SO good?

Dynamic Behaviour!

No help from the type system

Closing over state is not tracked

Invisible Mailbox

Actors? Futures? Both? When?

Too low level

Patterns or good practices

Minimize work, delegate lengthy work

Pull don't push

Ask for flow control

Use functions to create behaviour, pass state

Use traits to isolate code

Use different dispatchers for different sections of the app

Anatomy of an application

Simple Seed

Distributed Workers

Misc

Data locality

Modeling data as an actor? CQRS like

References

Akka

Actor model

Typesafe Activator

Hewitt's original paper


text:
namespace google {
namespace protobuf {

// When running unittests, get the directory containing the source code.
string TestSourceDir();

// When running unittests, get a directory where temporary files may be
// placed.
string TestTempDir();

// Capture all text written to stdout or stderr.
void CaptureTestStdout();
void CaptureTestStderr();

// Stop capturing stdout or stderr and return the text captured.
string GetCapturedTestStdout();
string GetCapturedTestStderr();

// For use with ScopedMemoryLog::GetMessages().  Inside Google the LogLevel
// constants don't have the LOGLEVEL_ prefix, so the code that used
// ScopedMemoryLog refers to LOGLEVEL_ERROR as just ERROR.
#undef ERROR  // defend against promiscuous windows.h
static const LogLevel ERROR = LOGLEVEL_ERROR;
static const LogLevel WARNING = LOGLEVEL_WARNING;

// Receives copies of all LOG(ERROR) messages while in scope.  Sample usage:
//   {
//     ScopedMemoryLog log;  // constructor registers object as a log sink
//     SomeRoutineThatMayLogMessages();
//     const vector<string>& warnings = log.GetMessages(ERROR);
//   }  // destructor unregisters object as a log sink
// This is a dummy implementation which covers only what is used by protocol
// buffer unit tests.
class ScopedMemoryLog {
 public:
  ScopedMemoryLog();
  virtual ~ScopedMemoryLog();

  // Fetches all messages with the given severity level.
  const vector<string>& GetMessages(LogLevel error);

 private:
  map<LogLevel, vector<string> > messages_;
  LogHandler* old_handler_;

  static void HandleLog(LogLevel level, const char* filename, int line,
                        const string& message);

  static ScopedMemoryLog* active_log_;

  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ScopedMemoryLog);
};

}  // namespace protobuf
}  // namespace google

#endif  // GOOGLE_PROTOBUF_GOOGLETEST_H__


text:
<?php

namespace MusicBrainz\Value;

/**
 * A list of instruments
 */
class InstrumentList extends ValueList
{
    /**
     * Constructs a list of instruments.
     *
     * @param array $instruments An array instrument arrays
     */
    public function __construct(array $instruments = [])
    {
        parent::__construct(
            array_map(
                function ($instrument) {
                    return new Instrument($instrument);
                },
                $instruments
            )
        );
    }

    /**
     * Returns the class name of the list elements.
     *
     * @return string
     */
    public static function getType(): string
    {
        return Instrument::class;
    }
}


text:
package org.mercycorps.translationcards.media;

import android.widget.ProgressBar;

import org.mercycorps.translationcards.exception.AudioFileException;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

/**
 * Created by karthikbalasubramanian on 3/28/16.
 */
public class DecoratedMediaManager {
    private final int INITIAL_DELAY = 0;
    private final int PERIOD = 100;
    private ProgressBar progressBar;
    private ScheduledFuture scheduledFuture;
    private static final int RESET_PROGRESS_BAR = 0;
    private String filename;
    private AudioPlayerManager audioPlayerManager;
    private ScheduledExecutorService scheduledExecutorService;

    @Inject
    public DecoratedMediaManager(AudioPlayerManager audioPlayerManager, ScheduledExecutorService scheduledExecutorService) {
        this.audioPlayerManager = audioPlayerManager;
        this.scheduledExecutorService = scheduledExecutorService;
    }

    //// TODO: 3/28/16 There is an implicit order here. Play has to be called before you can call maxDuration or getCurrentPosition.
    public void play(String filename, ProgressBar progressBar, boolean isAsset) throws AudioFileException {
        this.progressBar = progressBar;
        this.filename = filename;
        audioPlayerManager.play(this.filename, isAsset);
        progressBar.setMax(audioPlayerManager.getMaxDuration());
        schedule();
    }

    private void schedule() {
        scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(createRunnable(), INITIAL_DELAY, PERIOD, TimeUnit.MILLISECONDS);
    }

    //// TODO: 3/28/16 Figure out how to unit test this!
    private Runnable createRunnable() {
        return new Runnable() {
            @Override
            public void run() {
                if(!isPlaying()) stop();
                else progressBar.setProgress(audioPlayerManager.getCurrentPosition());
            }
        };
    }

    public void stop() {
        if (playHasNotBeenCalled()) return;
        audioPlayerManager.stop();
        progressBar.setProgress(RESET_PROGRESS_BAR);
        scheduledFuture.cancel(true);
    }

    private boolean playHasNotBeenCalled() {
        return progressBar == null || scheduledFuture == null;
    }

    public ScheduledFuture getScheduledFuture() {
        return scheduledFuture;
    }

    public boolean isPlaying() {
        return audioPlayerManager.isPlaying();
    }

    public boolean isCurrentlyPlayingSameCard(String fileName) {
        return isSameAudioFile(fileName) && isPlaying();
    }

    private boolean isSameAudioFile(String fileName) {
        return fileName != null && fileName.equals(this.filename);
    }
}


text:
<!DOCTYPE html>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Extending Schemas - schema.org</title>
    <meta name="description" content="Schema.org is a set of extensible schemas that enables webmasters to embed
    structured data on their web pages for use by search engines and other applications." />
    <link rel="stylesheet" type="text/css" href="schemaorg.css" />

					schema.org

                      Documentation

                      Schemas

                      Home

Schema.org Extensions

We have two kinds of named extension to schema.org: 'hosted' and 'external'.  This is in addition to the ongoing practice of extending
  schema.org via the changes and improvements included in every release.

Hosted extensions are managed and reviewed as part of the schema.org project itself.
External extensions are managed and reviewed by other groups. See the schemas page for a listing.

Motivation

    Schema.org provides a core, basic vocabulary for describing the kind of
    entities the most common web applications need. There is often a
    need for more specialized and/or deeper vocabularies, that build
    upon the core. The extension mechanisms facilitate the creation of
    such additional vocabularies.

    With most extensions, we expect that some small frequently used
    set of terms will be in core schema.org, with a long tail of more
    specialized terms in the extension.

Types of Extensions

There are two kinds of extensions: reviewed/hosted extensions and external extensions. Both kinds of extensions typically add subclasses and properties to the core. Properties may be added to existing and/or new classes. More generally, they are an overlay on top of the core, and so they may add domains/ranges, superclasses, etc. as well. Extensions have to be consistent with the core schema.org. Every item in the core (i.e., http://schema.org/) is also in every extension. Extensions might overlap with each other in concepts (e.g., two extensions defining terms for financial institutions, one calling it FinancialBank and other calling it FinancialInstitution), but we should not have the same term being reused to mean something completely different (e.g., we should not have two extensions, one using Bank to mean river bank and the other using Bank to mean financial institution).

Reviewed/hosted Extensions

Each reviewed extension (say, e1), gets its own chunk of schema.org namespace: e1.schema.org in the schema.org website. The items in that extension are created and maintained by the creators of that extension.  Reviewed extensions are very different from proposals. A proposal, if accepted, with modifications could either go into the core or become a reviewed extension.

A reviewed extension is something that has been looked at and discussed by the community, albeit not as much as something in the core. We also expect a reviewed extension to have strong community support, preferably in the form of a few deployments.

External Extensions

Sometimes there might be a need for a third party (such as an app
developer) to create extensions specific to their application. For
example, Pinterest might want to extend the schema.org concept of
'Sharing' with 'Pinning'. In such a case, they can create
schema.pinterest.com and put up their extensions, specifying how it
links with core schema.org. We will refer to these as external
extensions.

There are also cases where a third party might want to host a
broadly applicable extension themselves. In such cases, the extension
may go through the kind of feedback process that reviewed extensions
go through, but can be hosted on the third party's site.

How it works for webmasters

 All of Schema.org core and all of the reviewed extensions are available from the schema.org website. Each extension is linked to from each of the touch points it has with the core. So, if an extension (say, having to do with Legal stuff) creates LegalPerson which is a subclass of Person, the Person will link to LegalPerson.

Reviewed extensions are identified in the website with an appropriate URL prefix.  For example bib.schema.org for bibliographic terms, auto.schema.org for automotive terms.  These URL prefixes are only applicable to the documentation of terms within an extention.  The Schema.org vocabulary and its reviewed extensions are defined in a flat namespace.  ie. All terms, in the core vocabulary and extensions, within the vocabulary have a http://schema.org based cannonical URI.  For example the MotorizedBicycle type defined in the auto.schema.org extension and documented on the http://auto.schema.org/MotorizedBicycle page, has a cannonical URI of http://schema.org/MotorizedBicycle.  The cannonical URI is the value that is used when applying Schema.org markup.

<div itemscope itemtype="http://schema.org/MotorizedBicycle">

What does someone creating an extension need to do

  Extension creators to not have to worry about running a website for their extension. Once the extension is approved, they simply upload a file defining their extension and associated examples into an agreed directory for that extension on github. Changes are made through the same mechanism.

More information about proposing and implementng extensions can be found in How We Work.

Historical Note:

This page introduces the new extension mechanism adopted in May
    2015. Documentation on the previous 'slash-based' extension mechanism
    has been archived.

  Terms and conditions


text:
/**
 *+
 *  ArgumentsHelp.java
 *	1.0.0	Mar 18, 2014  Leo Hinterlang
 *-
 */
package com.fidelis.argface;

import java.util.ArrayList;
import java.util.List;

/**
 * ArgFace Help Class.
 *
 * @version 1.0.0
 * @author Leo Hinterlang
 *
 */
public class ArgHelp {
    private ArgList         argList;
    private List<ArgOption> optionList;
    private boolean         suppressHelp;
    private String          usageText;
    private String          versionText;
    private String          aboutText;
    private String          helpText;
    private List<ArgNode>   problemUsage = new ArrayList<ArgNode>();
    private List<String>    problemText  = new ArrayList<String>();

    /**
     * Sets the argument list.
     *
     * @param argList the argument list
     */
    public void setArgList (ArgList argList) {
        this.argList = argList;
    }

    /**
     * Sets the option list.
     *
     * @param optionList the option list
     */
    public void setOptionList (List<ArgOption> optionList) {
        this.optionList = optionList;
    }

    /**
     * Sets the "suppressHelp" operating mode.
     *
     * @param suppressHelp {@code true} to suppress help
     */
    public void setSuppressHelp (boolean suppressHelp) {
        this.suppressHelp = suppressHelp;
    }

    /**
     * Sets the "version" text.
     *
     * @param versionText the version text
     */
    public void setVersionText (String versionText) {
        this.versionText = versionText;
    }

    /**
     * Sets the "about" text.
     *
     * @param aboutText the aboutText to set
     */
    public void setAboutText (String aboutText) {
        this.aboutText = aboutText;
    }

    /**
     * Sets the "help" text.
     *
     * @param helpText the help text
     */
    public void setHelpText (String helpText) {
        this.helpText = helpText;
    }

    /**
     * Prints the "usage" text.
     *
     * @return {@code true} if the usage text was printed
     */
    public boolean printUsage () {
        if (suppressHelp) {
            return false;
        }
        return outputUsage();
    }

    /**
     * Outputs the "usage" text.
     *
     * @return {@code true} if the usage text was output
     */
    public boolean outputUsage () {
        if (usageText == null) {
            usageText = buildUsageText();
        }
        if (usageText == null) {
            return false;
        }
        System.out.println(usageText);
        return true;
    }

    /**
     * Prints the "help" text.
     *
     * @return {@code true} if the help text was printed
     */
    public boolean printHelp () {
        if (suppressHelp) {
            return false;
        }
        return outputHelp();
    }

    /**
     * Outputs the "help" text.
     *
     * @return {@code true} if the help text was output
     */
    public boolean outputHelp () {
        int maxNameLength = 0;
        int maxAltLength = 0;
        int maxArgLength = 0;
        int nameLength = 0;
        int altLength = 0;
        int argLength = 0;
        for (ArgOption opt : optionList) {
            String name = opt.getName();
            String altName = opt.getAltName();
            String argName = opt.getArgName();
            nameLength = name.length();
            if (nameLength > 1) {
                ++nameLength;
                if (altName == null) {
                    altName = name;
                    nameLength = 1;
                }
            }
            ++nameLength;
            if (nameLength > maxNameLength) {
                maxNameLength = nameLength;
            }
            if (altName != null) {
                altLength = altName.length() + 5;
                if (altLength > maxAltLength) {
                    maxAltLength = altLength;
                }
            }
            if (argName != null) {
                argLength = argName.length() + 2;
                if (argLength > maxArgLength) {
                    maxArgLength = argLength;
                }
            }
        }
        String nameFormat = maxNameLength == 0 ? "%s" : "%" + maxNameLength + "s";
        String altFormat =  maxAltLength == 0 ? "%s" : "%-" + maxAltLength + "s";
        String argFormat = maxArgLength == 0 ? "%s" : "%-" + maxArgLength + "s";
        outputUsage();
        System.out.println("Options:");
        for (ArgOption opt : optionList) {
            String name = opt.getName();
            String altName = opt.getAltName();
            String argName = opt.getArgName();
            String nDash = "-";
            String comma = ", ";
            if (name.length() > 1) {
                nDash = "--";
                if (altName == null) {
                    altName = name;
                    name = "";
                    nDash = "";
                    comma = "  ";
                }
            }
            StringBuilder sb = new StringBuilder("  ");
            sb.append(String.format(nameFormat, nDash + name));
            if (altName != null) {
                String aDash = altName.length() == 1 ? "-" : "--";
                sb.append(String.format(altFormat, comma + aDash + altName));
            } else {
                sb.append(String.format(altFormat, " "));
            }
            if (argName != null) {
                String arg = opt.isArgOptional() ? "[" + argName + "]"
                                                 : "<" + argName + ">";
                sb.append(String.format(argFormat, arg));
            } else {
                sb.append(String.format(argFormat, " "));
            }
            String help = opt.getHelp();
            if (help != null) {
                String [] parts = help.split("\\s");
                int slen = 0;
                int position = sb.length();
                for (String s : parts) {
                    if ((sb.length() - slen + s.length()) > 80) {
                        sb.append('\n');
                        slen = sb.length();
                        for (int n = 0; n <= position; n++) {
                            sb.append(' ');
                        }
                    } else {
                        sb.append(' ');
                    }
                    sb.append(s);
                }
            }
            System.out.println(sb);
        }
        if (helpText != null) {
            System.out.println();
            System.out.println(helpText);
        }
        return true;
    }

    /**
     * Prints the "version" text.
     *
     * @return {@code true} if the version text was printed
     */
    public boolean printVersion () {
        if (suppressHelp) {
            return false;
        }
        return outputVersion();
    }

    /**
     * Outputs the "version" text if there is any.
     *
     * @return {@code true} if the version text was output
     */
    public boolean outputVersion () {
        if (versionText == null) {
            return false;
        }
        System.out.println(versionText);
        return true;
    }

    /**
     * Prints the "about" text.
     *
     * @return {@code true} if the about text was printed
     */
    public boolean printAbout () {
        if (suppressHelp) {
            return false;
        }
        return outputAbout();
    }

    /**
     * Outputs the "about" text if there is any.
     *
     * @return {@code true} if the about text was output
     */
    public boolean outputAbout () {
        if (aboutText == null) {
            return false;
        }
        System.out.println(aboutText);
        return true;
    }

    public void initProblems () {
        problemUsage.clear();
        problemText.clear();
    }

    public void addProblem (ArgNode base, String text) {
        problemUsage.add(base);
        problemText.add(text);
    }

    public void printProblems () {
        int size = problemUsage.size();
        if (size == 0) {
            System.out.println("No help for this problem");
            return;
        }
        if (size == 1) {
            System.out.println("Possible problem:");
        } else {
            System.out.println("Possible problems:");
        }
        for (int n = 0; n < size; n++) {
            ArgNode base = problemUsage.get(n);
            String text = problemText.get(n);
            String usageSpec = argList.buildUsageSpec(base);
            System.out.println("  " + usageSpec);
            System.out.println("  " + text);
            System.out.println();
        }
    }

    private String buildUsageText () {
        if (argList != null) {
            return argList.buildUsage();
        }
        return null;
    }

}


text:
<?php

namespace Symfony\Component\Intl\Tests\Data\Provider;

use Symfony\Component\Intl\Data\Provider\LocaleDataProvider;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Intl\Locale;

/**
 * @author Bernhard Schussek <bschussek@gmail.com>
 *
 * @group legacy
 */
abstract class AbstractLocaleDataProviderTest extends AbstractDataProviderTest
{
    /**
     * @var LocaleDataProvider
     */
    protected $dataProvider;

    protected function setUp()
    {
        parent::setUp();

        $this->dataProvider = new LocaleDataProvider(
            $this->getDataDirectory().'/'.Intl::LOCALE_DIR,
            $this->createEntryReader()
        );
    }

    abstract protected function getDataDirectory();

    public function testGetLocales()
    {
        $this->assertSame($this->getLocales(), $this->dataProvider->getLocales());
    }

    public function testGetLocaleAliases()
    {
        $this->assertSame($this->getLocaleAliases(), $this->dataProvider->getAliases());
    }

    /**
     * @dataProvider provideLocales
     */
    public function testGetNames($displayLocale)
    {
        $locales = array_keys($this->dataProvider->getNames($displayLocale));

        sort($locales);

        // We can't assert on exact list of locale, as there's too many variations.
        // The best we can do is to make sure getNames() returns a subset of what getLocales() returns.
        $this->assertNotEmpty($locales);
        $this->assertEmpty(array_diff($locales, $this->getLocales()));
    }

    public function testGetNamesDefaultLocale()
    {
        Locale::setDefault('de_AT');

        $this->assertSame(
            $this->dataProvider->getNames('de_AT'),
            $this->dataProvider->getNames()
        );
    }

    /**
     * @dataProvider provideLocaleAliases
     */
    public function testGetNamesSupportsAliases($alias, $ofLocale)
    {
        // Can't use assertSame(), because some aliases contain scripts with
        // different collation (=order of output) than their aliased locale
        // e.g. sr_Latn_ME => sr_ME
        $this->assertEquals(
            $this->dataProvider->getNames($ofLocale),
            $this->dataProvider->getNames($alias)
        );
    }

    /**
     * @dataProvider provideLocales
     */
    public function testGetName($displayLocale)
    {
        $names = $this->dataProvider->getNames($displayLocale);

        foreach ($names as $locale => $name) {
            $this->assertSame($name, $this->dataProvider->getName($locale, $displayLocale));
        }
    }

    public function testGetNameDefaultLocale()
    {
        Locale::setDefault('de_AT');

        $names = $this->dataProvider->getNames('de_AT');

        foreach ($names as $locale => $name) {
            $this->assertSame($name, $this->dataProvider->getName($locale));
        }
    }
}


text:
namespace PXPAgent {
namespace Modules {

  class File : public PXPAgent::Util::BoltModule, public PXPAgent::Util::Purgeable {
    public:
      File(const std::vector<std::string>& master_uris,
                  const std::string& ca,
                  const std::string& crt,
                  const std::string& key,
                  const std::string& crl,
                  const std::string& proxy,
                  uint32_t download_connect_timeout,
                  uint32_t download_timeout,
                  std::shared_ptr<ModuleCacheDir> module_cache_dir,
                  std::shared_ptr<ResultsStorage> storage);

      /// Utility to purge files from the cache_dir that have surpassed the ttl.
      /// If a purge_callback is not specified, the boost filesystem's remove_all() will be used.
      /// Returns number of directories purged.
      unsigned int purge(
          const std::string& ttl,
          std::vector<std::string> ongoing_transactions,
          std::function<void(const std::string& dir_path)> purge_callback = nullptr) override;

    private:
      std::vector<std::string> master_uris_;

      uint32_t file_download_connect_timeout_, file_download_timeout_;

      leatherman::curl::client client_;

      // callAction is normally implemented in the BoltModule base class. However:
      // DownloadFile will not execute any external processes, so it does not need the
      // overhead from callAction to parse blocking/non-blocking and call extrenal
      // processes.
      //
      // DownloadFile re-implements callAction to provide the functionality to download
      // files.
      ActionResponse callAction(const ActionRequest& request) override;

      // Since DownloadFile overrides callAction there's no reason to define
      // buildCommandObject (since it will never be called)
      Util::CommandObject buildCommandObject(const ActionRequest& request) override {
        throw Module::ProcessingError(leatherman::locale::format("DownloadFile module does not implement buildCommandObject!"));
        return Util::CommandObject {
          "",
          {},
          {},
          "",
          nullptr
        };
      }
  };

}  // namespace Modules
}  // namespace PXPAgent

 #endif  // SRC_MODULES_FILE_H_


text:
from pandac.PandaModules import *
from direct.gui.DirectGui import *
from SCObject import SCObject
from SCElement import SCElement
from SCMenu import SCMenu
import types

class SCMenuHolder(SCElement):
    N = 0.9
    DefaultFrameColor = (0,
     0,
     0,
     1.0 - N)
    del N
    MenuColorScaleDown = 0.95

    def __init__(self, title, menu = None):
        SCElement.__init__(self)
        self.title = title
        scGui = loader.loadModel(SCMenu.GuiModelName)
        self.scArrow = scGui.find('**/chatArrow')
        self.menu = None
        self.setMenu(menu)
        return

    def destroy(self):
        if self.menu is not None:
            self.menu.destroy()
            self.menu = None
        SCElement.destroy(self)
        return

    def setTitle(self, title):
        self.title = title
        self.invalidate()

    def getTitle(self):
        return self.title

    def setMenu(self, menu):
        if self.menu is not None:
            self.menu.destroy()
        self.menu = menu
        if self.menu is not None:
            self.privAdoptSCObject(self.menu)
            self.menu.setHolder(self)
            self.menu.reparentTo(self, 1)
            self.menu.hide()
        self.updateViewability()
        return

    def getMenu(self):
        return self.menu

    def showMenu(self):
        if self.menu is not None:
            cS = SCMenuHolder.MenuColorScaleDown
            self.menu.setColorScale(cS, cS, cS, 1)
            self.menu.enterVisible()
            self.menu.show()
        return

    def hideMenu(self):
        if self.menu is not None:
            self.menu.hide()
            self.menu.exitVisible()
        return

    def getMenuOverlap(self):
        if self.parentMenu.isTopLevel():
            return self.getTopLevelOverlap()
        else:
            return self.getSubmenuOverlap()

    def getMenuOffset(self):
        xOffset = self.width * (1.0 - self.getMenuOverlap())
        return Point3(xOffset, 0, 0)

    def onMouseClick(self, event):
        SCElement.enterActive(self)
        self.parentMenu.memberSelected(self)

    def enterActive(self):
        SCElement.enterActive(self)
        self.showMenu()
        if hasattr(self, 'button'):
            r, g, b = self.getColorScheme().getMenuHolderActiveColor()
            a = self.getColorScheme().getAlpha()
            self.button.frameStyle[DGG.BUTTON_READY_STATE].setColor(r, g, b, a)
            self.button.updateFrameStyle()
        else:
            self.notify.warning('SCMenuHolder has no button (has finalize been called?).')

    def exitActive(self):
        SCElement.exitActive(self)
        self.hideMenu()
        self.button.frameStyle[DGG.BUTTON_READY_STATE].setColor(*SCMenuHolder.DefaultFrameColor)
        self.button.updateFrameStyle()

    def getDisplayText(self):
        return self.title

    def updateViewability(self):
        if self.menu is None:
            self.setViewable(0)
            return
        isViewable = False
        for child in self.menu:
            if child.isViewable():
                isViewable = True
                break

        self.setViewable(isViewable)
        return

    def getMinSubmenuWidth(self):
        parentMenu = self.getParentMenu()
        if parentMenu is None:
            myWidth, myWeight = self.getMinDimensions()
        else:
            myWidth = parentMenu.getWidth()
        return 0.15 + myWidth * self.getMenuOverlap()

    def getMinDimensions(self):
        width, height = SCElement.getMinDimensions(self)
        width += 1.0
        return (width, height)

    def invalidate(self):
        SCElement.invalidate(self)
        if self.menu is not None:
            self.menu.invalidate()
        return

    def finalize(self, dbArgs = {}):
        if not self.isDirty():
            return
        r, g, b = self.getColorScheme().getArrowColor()
        a = self.getColorScheme().getAlpha()
        self.scArrow.setColorScale(r, g, b, a)
        if self.menu is not None:
            self.menu.setPos(self.getMenuOffset())
        if self.isActive():
            r, g, b = self.getColorScheme().getMenuHolderActiveColor()
            a = self.getColorScheme().getAlpha()
            frameColor = (r,
             g,
             b,
             a)
        else:
            frameColor = SCMenuHolder.DefaultFrameColor
        args = {'image': self.scArrow,
         'image_pos': (self.width - 0.5, 0, -self.height * 0.5),
         'frameColor': frameColor}
        args.update(dbArgs)
        SCElement.finalize(self, dbArgs=args)
        return

    def hasStickyFocus(self):
        return 1

    def privSetSettingsRef(self, settingsRef):
        SCObject.privSetSettingsRef(self, settingsRef)
        if self.menu is not None:
            self.menu.privSetSettingsRef(settingsRef)
        return

    def invalidateAll(self):
        SCObject.invalidateAll(self)
        if self.menu is not None:
            self.menu.invalidateAll()
        return

    def finalizeAll(self):
        SCObject.finalizeAll(self)
        if self.menu is not None:
            self.menu.finalizeAll()
        return


text:
@implementation WeekDayUtil {

}

+ (NSDate *)dateForNextWeekday:(NSInteger) weekday fromDate:(NSDate *) fromDate {
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit
                                                     fromDate:fromDate];
    NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
    NSInteger diff = weekday - weekdayComponents.weekday;
    if (diff <= 0) diff += 7;
    [componentsToSubtract setDay:diff];
    NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract
                                         toDate:fromDate options:0];
    return beginningOfWeek;
}
@end


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<title>com.douglaswhitehead.model.digitaldata.privacy</title>
<meta name="date" content="2015-11-09">
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">

com.douglaswhitehead.model.digitaldata.privacy

Interfaces

AccessCategory

AccessCategory.Builder

Privacy

Privacy.Builder

Classes

AccessCategoryImpl

AccessCategoryImpl.Builder

PrivacyImpl

PrivacyImpl.Builder


text:
Welcome to the WebGL Conformance Test Suite
===========================================

Note: Before adding a new test or editing an existing test
[please read these guidelines](test-guidelines.md).

This is the initial release of the WebGL conformance test suite.

NOTE TO USERS: Unless you are a WebGL implementor, there is no need to submit
a conformance result using this process.  Should you discover bugs in your
browser's WebGL implementation, either via this test suite or otherwise,
please report them through your browser vendor's bug tracking system.

FOR WEBGL IMPLEMENTORS: Please follow the instructions below to create
a formal conformance submission.

1. Open webgl-conformance-tests.html in your target browser

2. Press the "run tests" button

3. At the end of the run, press "display text summary"

4. Verify that the User Agent and WebGL renderer strings identify your browser and target correctly.

5. Copy the contents of the text summary (starting with "WebGL Conformance Test Results") and send via email to
   webgl_conformance_submissions@khronos.org

Please see CONFORMANCE_RULES.txt in this directory for guidelines
about what constitutes a conformant WebGL implementation.

Usage Notes:
------------

There are various URL options you can pass in.

    run:         Set to 1 to start the tests automatically

                 Example: webgl-conformance-tests.html?run=1

    version:     Set to the version of the harness you wish to run. Tests
                 at this version or below will be run

                 Example: webgl-conformance-tests.html?version=1.3.2

    minVersion:  Set to the minimum version of each test to include. Only tests
                 at this version or above will be included.

                 Example: webgl-conformance-tests.html?minVersion=1.3.2

    fast:        Only run tests not marked with --slow

                 Example: webgl-conformance-tests.html?fast=true

    skip:        Comma separated list of regular expressions of which tests to skip.

                 Example: webgl-conformance-tests.html?skip=glsl,.*destruction\.html

    include:     Comma separated list of regular expressions of which tests to include.

                 Example: webgl-conformance-tests.html?include=glsl,.*destruction\.html

History
-------

- 2011/02/24: Version 1.0.0
- 2012/02/23: Version 1.0.1
- 2012/03/20: Version 1.0.2 (beta)


text:
SYNONYM

#### According to
GRIN Taxonomy for Plants

#### Published in
Biltmore Bot. Stud. 1:16. 1901

#### Original name
null

### Remarks
null


text:
import click
import configparser
import logging
import os
import pickle
import requests
import sys
import urllib

from bs4 import BeautifulSoup
from bs4 import Tag

import twitter
from twitter import *
# pip install twitter
#https://pypi.python.org/pypi/twitter
#https://github.com/sixohsix/twitter/tree
#http://mike.verdone.ca/twitter/
from html.parser import HTMLParser

from configMod import *
from moduleContent import *
from moduleQueue import *

class moduleTwitter(Content,Queue):

    def __init__(self):
        super().__init__()
        self.user = None
        self.tc = None
        self.service = None

    def setClient(self, twitterAC):
        logging.info("     Connecting Twitter")
        self.service = 'Twitter'
        try:
            logging.info("     Twitter Acc %s"%str(twitterAC))
            config = configparser.ConfigParser()
            config.read(CONFIGDIR + '/.rssTwitter')

            if isinstance(twitterAC, str):
                self.user = twitterAC
            else:
                self.user = twitterAC[1][1]
            logging.info("     Twitter User %s"%str(self.user))
            try:
                CONSUMER_KEY = config.get(self.user, "CONSUMER_KEY")
            except:
                CONSUMER_KEY = config.get("appKeys", "CONSUMER_KEY")
            try:
                CONSUMER_SECRET = config.get(self.user, "CONSUMER_SECRET")
            except:
                CONSUMER_SECRET = config.get("appKeys", "CONSUMER_SECRET")
            TOKEN_KEY = config.get(self.user, "TOKEN_KEY")
            TOKEN_SECRET = config.get(self.user, "TOKEN_SECRET")

            try:
                authentication = OAuth(
                            TOKEN_KEY,
                            TOKEN_SECRET,
                            CONSUMER_KEY,
                            CONSUMER_SECRET)
                t = Twitter(auth=authentication)
            except:
                logging.warning("Twitter authentication failed!")
                logging.warning("Unexpected error:", sys.exc_info()[0])
        except:
            logging.warning("Account not configured")
            logging.warning("Unexpected error:", sys.exc_info()[0])
            t = None

        self.tc = t

    def getClient(self):
        return self.tc

    def setPosts(self):
        logging.info("  Setting posts")
        self.posts = []
        #tweets = self.tc.statuses.home_timeline()
        try:
            self.posts = self.tc.statuses.user_timeline()
        except:
            self.posts = []

        #outputData = {}
        serviceName = 'Twitter'
        #outputData[serviceName] = {'sent': [], 'pending': []}
        #for post in self.getPosts():
        #    #print(post)
        #    url = 'https://twitter.com/' + post['user']['screen_name'] + '/status/' + str(post['id'])
        #    if 'urls' in post:
        #        link = post['urls'][0]['expanded_url']
        #    else:
        #        link = ''

        #    outputData[serviceName]['sent'].append((post['text'], url,
        #            post['user']['screen_name'],
        #            post['created_at'], link,'','','',''))

        #self.postsFormatted = outputData

    def publishPost(self, post, link='', comment=''):
        logging.info("     Publishing in Twitter...")
        if comment != None:
            post = comment + " " + post
        h = HTMLParser()
        post = h.unescape(post)
        res = None
        try:
            logging.info("     Publishing: %s" % post)
            post = post[:(240 - (len(link) + 1))]
            res = self.tc.statuses.update(status=post+" " + link)

            if res:
                logging.debug("Res: %s" % res)
                urlTw = "https://twitter.com/%s/status/%s" % (self.user, res['id'])
                logging.info("     Link: %s" % urlTw)
                return(post +'\n'+urlTw)

        except twitter.api.TwitterHTTPError as twittererror:
            for error in twittererror.response_data.get("errors", []):
                logging.info("      Error code: %s" % error.get("code", None))
            return(self.report('Twitter', post, link, sys.exc_info()))
        except:
            logging.info("Fail!")
            return(self.report('Twitter', post, link, sys.exc_info()))

    def getPostTitle(self, post):
        if 'text' in post:
            return(post['text'])
        else:
            return ''

    def getPostLink(self, post):
        if 'id_str' in post:
            return('https://twitter.com/{}/status/{}'.format(self.user, post['id_str']))
        else:
            return ''

    def getPostUrl(self, post):
        logging.debug(post)
        if (('urls' in post['entities']) and (post['entities']['urls'])):
            return(post['entities']['urls'][0]['expanded_url'])
        else:
            return ''

    def search(self, text):
        logging.debug("     Searching in Twitter...")
        try:
            res = self.tc.search.tweets(q=text)

            if res:
                logging.debug("Res: %s" % res)
                return(res)
        except:
            return(self.report('Twitter', text, sys.exc_info()))

def main():

    logging.basicConfig(stream=sys.stdout,
            level=logging.INFO,
            format='%(asctime)s %(message)s')

    import moduleTwitter
    tw = moduleTwitter.moduleTwitter()

    tw.setClient('fernand0')

    print("Testing posts")
    tw.setPosts()
    for i, tweet in enumerate(tw.getPosts()):
        print("{}) {}".format(i,tweet))
        #print("@%s: %s" %(tweet[2], tweet[0]))

    print("Testing title and link")

    for post in tw.getPosts():
        title = tw.getPostTitle(post)
        link = tw.getPostLink(post)
        url = tw.getPostUrl(post)
        print("Title: {}\nLink: {}\nUrl:{}\n".format(title,link,url))

    res = tw.search('url:fernand0')

    for tt in res['statuses']:
        print(tt)
        print('- @{0} {1} https://twitter.com/{0}/status/{2}'.format(tt['user']['name'], tt['text'], tt['id_str']))
    sys.exit()

    tw.publishPost("Inscripciones 2019 | Congreso Web", "http://congresoweb.es/cw19/inscripciones/", '')

if __name__ == '__main__':
    main()


text:
package sls

import (
	"os"
	"testing"

	"github.com/golang/glog"
	"github.com/stretchr/testify/suite"
)

func TestProject(t *testing.T) {
	suite.Run(t, new(ProjectTestSuite))
	glog.Flush()
}

type ProjectTestSuite struct {
	suite.Suite
	endpoint        string
	accessKeyID     string
	accessKeySecret string
	client          Client
}

func (s *ProjectTestSuite) SetupTest() {
	s.endpoint = os.Getenv("LOG_TEST_ENDPOINT")
	s.accessKeyID = os.Getenv("LOG_TEST_ACCESS_KEY_ID")
	s.accessKeySecret = os.Getenv("LOG_TEST_ACCESS_KEY_SECRET")
	s.client = Client{
		Endpoint:        s.endpoint,
		AccessKeyID:     s.accessKeyID,
		AccessKeySecret: s.accessKeySecret,
		SecurityToken:   "",
	}
}

func (s *ProjectTestSuite) TestCheckProjectExist() {
	projectName := os.Getenv("LOG_TEST_PROJECT")
	exist, err := s.client.CheckProjectExist(projectName)
	s.Nil(err)
	s.True(exist)
}

// func (s *ProjectTestSuite) TestUpdateProject() {
// 	projectName := os.Getenv("LOG_TEST_PROJECT")
// 	_, err := s.client.UpdateProject(projectName, "aliyun log go sdk test.")
// 	s.Nil(err)
// }


text:
using Newtonsoft.Json;

namespace Alexa.NET.Management.Package
{
    public class ExportStatusResponse
    {
        [JsonProperty("status")]
        public ExportStatus Status { get; set; }

        [JsonProperty("skill")]
        public ExportMetadata Skill { get; set; }
    }
}